繁体   English   中英

Python-Web抓取页面

[英]Python- WebScraping a page

我的代码应该是 go 进入网站,浏览 2 页,并打印出每行中的所有标题和 URL/href。

目前 - 我的代码很好地进入了这两个页面,但是它只打印出每页的第一个标题,而不是每行的每个标题。

该页面确实有一些 JavaScript,我想也许这就是为什么它没有在每一行中显示任何链接/url/hrefs 的原因? 理想情况下,id 喜欢打印每一行的 URL。

from selenium import webdriver
import time

driver = webdriver.Chrome()

for x in range (1,3):
    driver.get(f'https://www.abstractsonline.com/pp8/#!/9325/presentations/endometrial/{x}')
    time.sleep(3)
    page_source = driver.page_source
    eachrow=driver.find_elements_by_xpath("//li[@class='result clearfix']")
    for item in eachrow:
        title=driver.find_element_by_xpath("//span[@class='bodyTitle']").text
        print(title)

您在 for 循环中使用driver意味着您正在搜索整个页面 - 因此您将始终获得相同的元素。

您想从每个item中搜索。

for item in eachrow:
    title = item.find_element_by_xpath(".//span[@class='bodyTitle']").text

此外,如上所述的行中没有“URL” - 当您单击一行时,请求中会使用data-id属性。

<h1 class="name" data-id="1989" data-key="">

它将请求发送到https://www.abstractsonline.com/oe3/Program/9325/Presentation/694

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM