我正在尝试使用下面的代码抓取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。
我不知道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
来自view-source:https://www.rail.co.il/pages/trainsearchresultnew.aspx?FSID=4170&TSID=5000&Date=20190630&Hour=1000&IOT=true&IBA=false&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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句