【问题标题】:In my web-scraping, some divs don't appear在我的网络抓取中,一些 div 没有出现
【发布时间】:2019-06-30 16:00:30
【问题描述】:

我正在尝试使用此代码抓取 HTML 页面:

driver = webdriver.Chrome()
driver.get(url)
try:
    element = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.CLASS_NAME,
                                    "myclass")))  
    html = driver.page_source
    soup = bs(html, "lxml")
    print(html)
    dynamic_text = soup.find_all("div", {"class": "myclass"})  
except:
       print("Couldnt locate element")

html 页面已打开,但在我的 IDE 控制台中,我看到了异常消息。看起来,没有找到 class_name 为“myclass”的 div。但是,当我检查我得到的 html 页面时,我会在那里看到具有该类名的 div。

HTML 中的 div:

<div role="radio" data-ng-attr-id="{{radioId}}" data-ng-attr-tabindex="{{directToShow === strVm.data.selectedDirectToShow ? '0' : '-1'}}" data-ng-attr-aria-checked="{{directToShow === strVm.data.selectedDirectToShow ? 'true' : 'false'}}" class="trainBasicInfo ng-scope" data-ng-if="directToShow.date == undefined" data-ng-click="strVm.onSelectDirectToShow(directToShow, $event)" data-ng-class="{'active': directToShow === strVm.data.selectedDirectToShow}" id="railRadio_423" tabindex="-1" aria-checked="false">

我在WebDriverWait 中添加了评论,我看到了 print(html) 命令的输出。在打印的输出中我看不到 div,但是当我检查打开的 chrome 页面的检查时,我可以看到 div。

【问题讨论】:

标签: python selenium-webdriver web-scraping


【解决方案1】:

我不知道你用的是哪个class,但是用浏览器检查时的类,和源页面的类不一样:DOM是在加载页面源代码后被JavaScript修改的。

试试这个:

driver = webdriver.Chrome()
driver.get(url)
try:
    elements = WebDriverWait(driver, 20).until(
    EC.presence_of_all_elements_located((By.XPATH,
                                    "//div[contains(@class, 'trainBasicInfo ng-scope')]")))  
    # By.XPATH gives more flexibility
    for element in elements: 
        print(element)
except:
       # print("Couldnt locate element")
       raise  # except with no Exception specified is prohibited

来自inspect,使用 Chrom 开发工具:

来自view-source:https://www.rail.co.il/pages/trainsearchresultnew.aspx?FSID=4170&amp;TSID=5000&amp;Date=20190630&amp;Hour=1000&amp;IOT=true&amp;IBA=false&amp;TSP=1561835762832

输出如下:

30.06.2019 יום א'
00:46
רציף 1
19:12
19:58
רכבת 687
החלפה 19:44
תל אביב - ההגנה - רציף 3
רכבת 425
30.06.2019 יום א'
00:44
רציף 1
19:27
20:11
רכבת 689
החלפה 19:56
תל אביב - ההגנה - רציף 3
רכבת 529
30.06.2019 יום א'
00:42
רציף 1
19:57
20:39
רכבת 691
החלפה 20:26
תל אביב - ההגנה - רציף 3
רכבת 979
30.06.2019 יום א'
00:44
רציף 1
20:27
21:11
רכבת 693
החלפה 20:56
תל אביב - ההגנה - רציף 2
רכבת 531
30.06.2019 יום א'
00:44
רציף 1
21:27
22:11
רכבת 8695
החלפה 21:49
תל אביב - סבידור מרכז - רציף 3
רכבת 533

【讨论】:

  • 首先谢谢!那行得通。我使用的类名是:trainBasicInfo ng-scope。 II 使用了 By.class_name 而不是 by.xpath。知道为什么 xpath 有效而 class_name 无效吗?
  • DOM 中的class nametrainBasicInfo ng-scope active show-path
【解决方案2】:

如果你使用 Selenium,你应该试试这个:

driver = webdriver.Chrome()
driver.get(url)

element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME,"myclass")))

html = driver.page_source
dynamic_text = driver.find_elements_by_xpath('//div') #this will be a list of all divs on the page, they all will be selenium object

另外请记住,页面上的某些脚本生成的内容可能不存在,具体取决于您的驱动程序配置

【讨论】:

  • 问题不在于 find_elements 而在于 webdriverwait .. 似乎当 selenium 打开我的 chrome 浏览器并检查元素时,我可以找到 div 但在代码中找不到该 div ..
  • 如果您要查找的元素在 WebDriverWait() 之后加载,那么您将无法通过运行脚本找到它。您可以尝试做的是在脚本完成后不关闭驱动程序,并通过手动检查页面来查看脚本可以看到的内容。
  • 我做到了,正如我所说,在脚本打开的 chrome 浏览器中,我可以看到相关的 div.. 在我的脚本的打印输出中,我没有看到 div..跨度>
  • 然后尝试将 WebDriverWait() 目标从类更改为 xpath,WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.XPATH, '//div[@id= “我正在寻找的 div 之后的元素”]'))) 或者只是一起删除 WebDriverWait()
  • 没关系。 driver.page_source 没有显示该 div,而我可以在浏览器中看到它。这就是这里的问题..
猜你喜欢
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 2016-10-12
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 1970-01-01
相关资源
最近更新 更多