Search code examples
pythonseleniumscrapybloombergscrapy-splash

How to extract data from a secure website like bloomberg


I am trying to scrape item on this url:

"https://www.bloomberg.com/news/articles/2019-05-30/tesla-dealt-another-blow-as-barclays-sees-it-as-niche-carmaker"

I wanted to get the title and date publish only, any sample code that you can give to me even splash etc

so far what I tried is this

 def parse(self, response):
   yield scrapy.Request('https://www.bloomberg.com/news/articles/2019-05-30/tesla-dealt-another-blow-as-barclays-sees-it-as-niche-carmaker -H x-crawlera-use-https:1',
        headers={'X-Crawlera-Session': create,
        'X-Crawlera-Timeout': 40000,
        'X-Crawlera-Max-Retries': 5,
        'X-Crawlera-Cookies': disable,
        'X-Crawlera-Session': self.session_id
        },
        callback=self.parse_sub,
        )

 def parse_sub(self, response):
    response.xpath("//h1[@class = 'lede-text-v2__hed']").extract_first()
    response.xpath("//meta[@property = 'og:title']/@content").extract_first()
    response.xpath("//time[@class = 'article-timestamp']/@datetime").extract_first()
   print(response.text)

I am also using crawlera, but it keep detecting me as a robot


Solution

  • Using only to extract the title i.e. Tesla Dealt Another Blow When Barclays Calls It a ‘Niche Carmaker’ and the publishing date i.e. May 30, 2019, 5:26 PM GMT+5:30 you have to induce WebDriverWait for the visibility_of_element_located() and you can use the following solution:

    • Code Block

      from selenium import webdriver
      
      driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
      driver.get('https://www.bloomberg.com/news/articles/2019-05-30/tesla-dealt-another-blow-as-barclays-sees-it-as-niche-carmaker')
      print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='markets']//following:: h1[1]"))).get_attribute("innerHTML"))
      print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='markets']//following:: h1[1]//following::div[@class='lede-text-v2__times']/time[@itemprop='datePublished']"))).get_attribute("innerHTML"))
      driver.quit() 
      
    • Console Output:

      Tesla Dealt Another Blow When Barclays Calls It a ‘Niche Carmaker’
      May 30, 2019, 5:26 PM GMT+5:30
      
    • Note : You have to add the following imports :

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC