【问题标题】:WebScrape - Python/SeleniumWebScrape - Python/Selenium
【发布时间】:2021-03-10 21:14:22
【问题描述】:

我的代码进入一个网页,我想抓取该网页中每个列表的 href/HTML。

(此代码转到有 2 个的网站)

我尝试了 xpath 和 beautifulSoup,但它为我返回了一个空列表。

这是代码-

import time
from selenium import webdriver
import pandas as pd
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pandas as pd

from bs4 import BeautifulSoup
driver = webdriver.Chrome()
bracket=[]
driver.get('https://casehippo.com/spa/symposium/national-kidney-foundation-2021-spring-clinical-meetings/event/gallery/?search=Patiromer')
time.sleep(3)
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'html.parser')
eachRow=driver.find_element_by_partial_link_text('symposium')
print(eachRow.text)

【问题讨论】:

标签: python selenium web-scraping


【解决方案1】:

我刚刚运行了你提供的代码,BeautifulSoup 成功设置了所有页面源的汤变量:

soup = BeautifulSoup(page_source, 'html.parser')

在下一行:

eachRow=driver.find_element_by_partial_link_text('symposium')

异常已引发消息:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"symposium"}

似乎您使用了不正确的选择器,尝试使用,有点像:

element = driver.find_element_by_xpath("//a[@class='title ng-binding']")

编码我正在使用的代码:

import time
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()

try:
    bracket = []
    driver.get(
        'https://casehippo.com/spa/symposium/national-kidney-foundation-2021-spring-clinical-meetings/event/gallery/?search=Patiromer')
    time.sleep(3)
    page_source = driver.page_source
    soup = BeautifulSoup(page_source, 'html.parser')
    print(soup)
    element = driver.find_element_by_xpath("//a[@class='title ng-binding']")
    print(element.get_attribute('href'))
    elements = driver.find_elements_by_xpath("//a[@class='title ng-binding']")
    for el in elements:
       print(el.get_attribute('href'))
finally:
    driver.quit()

【讨论】:

  • @VoidS 什么错误,你能在这里发布吗?请检查一下,一切都在我身边
  • selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//a[@class='title ng-binding']"} 你能粘贴你的整个代码吗?
  • @VoidS 刚刚更新了答案,代码就在那里。尝试将睡眠值增加到 5 或 10
  • 谢谢,这适用于打印标题。然而,我试图获取这 2 个页面的 href/links/url
  • @VoidS 将 element.get_attribute('href') 添加到您的代码中并获得链接值
【解决方案2】:

更新代码:

import time
from selenium import webdriver
import pandas as pd
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pandas as pd

from bs4 import BeautifulSoup
driver = webdriver.Chrome()
bracket=[]
driver.get('https://casehippo.com/spa/symposium/national-kidney-foundation-2021-spring-clinical-meetings/event/gallery/?search=Patiromer')
time.sleep(3)
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'html.parser')
eachRow=driver.find_elements_by_xpath("//a[contains(@ui-sref,'symposium')]")
for row in eachRow:
    print(row.text)

如果有多个,则需要使用 find_elements(不是 find_element),然后遍历它们以查看它们的值。部分文本也不起作用,因为研讨会文本嵌入在另一个元素中,它不是常规文本,所以需要 xpath

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多