【问题标题】:Is this website protected against scraping? [closed]该网站是否受到保护以防刮擦? [关闭]
【发布时间】:2019-08-27 21:07:27
【问题描述】:

我正在尝试从该网站 scrape 年鉴: http://www.presanse.fr/CISME/annuaire.aspx 为了向您显示我需要抓取的信息,请单击“tous les services”,然后将显示一个列表,然后单击一个项目(例如 AST-BTP),一个页面将显示很多信息(我需要所有信息)。 我尝试检查代码,我注意到有 包含此信息,但我无法 scrape 它,我的脚本返回“无” 感谢您的帮助!

【问题讨论】:

  • 你应该使用像 selenium 这样的无头浏览器。
  • 好的,谢谢,我会的

标签: python beautifulsoup screen-scraping


【解决方案1】:

您想要的信息是使用 JavaScript 脚本加载的,简单地使用刮板发出请求是行不通的。

您将需要使用类似Selenium 的方式模拟单击按钮

【讨论】:

    【解决方案2】:

    如前所述,要做到这一点,除了美丽的汤外,您还需要使用硒。

    1) 在此处https://github.com/mozilla/geckodriver/releases下载 geckoDriver (fire fox)

    2) 解压exe并将其添加到您的系统路径

    3) 使用 pip install selenium

    安装 selenium

    4) 运行以下命令:

    from bs4 import BeautifulSoup
    from selenium import webdriver
    import time
    
    driver = webdriver.Firefox()
    driver.get('http://www.presanse.fr/CISME/annuaire.aspx')
    
    
    availbutton = driver.find_element_by_id('ctl00_cphMiddle_UC_RechercheParCarte1_linkTousLesServices')
    availbutton.click()
    time.sleep(2)
    
    html = driver.page_source
    soup = BeautifulSoup(html,'lxml')
    
    targetDiv = soup.find_all("div", {"class": "resultatTable"})
    targetsoup = BeautifulSoup(str(targetDiv),'lxml')
    for span in targetsoup:
        print(span.text)
    
    driver.close()
    

    您可以与之前动态创建的元素进行交互,也可以使用 button.click() 点击 DOM 元素。我添加了 2 秒的延迟以允许表格加载,因为我最初仍然得到空白而没有时间加载!

    【讨论】:

    • 感谢您的回答,为了删除每个页面中包含的信息,我使用了 button.click() 但文本不会显示在“span”标签中。这是我添加到您的代码中的代码:availbutton2 = driver.find_element_by_id('ctl00_cphMiddle_UC_RechercheParCarte1_UC_RechercheResultat1_rResultat_ctl00_pLigne')打印(目标购物车[0])
    猜你喜欢
    • 1970-01-01
    • 2018-07-24
    • 2015-02-18
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 2022-08-29
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多