如何从像彭博社这样的安全网站提取数据

2024-09-28 03:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从以下url上获取项目:

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

我只想出版书名和出版日期, 任何你能给我的示例代码,甚至splash等等

到目前为止,我尝试的是

^{pr2}$

我也在用crawlera,但它一直把我当成机器人


Tags: 项目httpscomurlaswwwanotherarticles
1条回答
网友
1楼 · 发布于 2024-09-28 03:19:47

仅使用来提取标题,即特斯拉在巴克莱称其为“利基汽车制造商”以及发布日期即2019年5月30日下午5:26 GMT+5:30您必须为visibility_of_element_located()归纳WebDriverWait,您可以使用以下解决方案:

  • 代码块

    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() 
    
  • 控制台输出:

    Tesla Dealt Another Blow When Barclays Calls It a ‘Niche Carmaker’
    May 30, 2019, 5:26 PM GMT+5:30
    
  • 注意:您必须添加以下导入:

    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
    

相关问题 更多 >

    热门问题