【问题标题】:Issue Scraping data from website that seems to change问题从似乎发生变化的网站上抓取数据
【发布时间】:2021-02-03 11:04:08
【问题描述】:

我正在尝试从https://gbr.milesplit.com/athletes/pro/5424599/stats 的 PR College 表中抓取数据 代码看起来像

prTable = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div/aside/div[4]/table')))

有时我得到的响应是正确的,它是一个包含个人记录的字符串。其他时候它只是从网页上抓取随机信息,有时它无法找到元素。我很困惑为什么它有时会起作用,但并非总是如此。有没有比 xpath 更好的方法,还是我使用了错误的标签?

【问题讨论】:

  • 你能用BeautifulSoup吗?
  • 我从未使用过 BS,在这种情况下我将如何使用它?
  • 我用beautifulsoup发布了一个例子
  • 如果站点是动态的,bs4 将无法完成。 @CMB 既然你说它会获取随机信息,我认为你不应该尝试使用Xpath 获取信息。尝试使用类名、id等获取你想要的东西。这种方式更精确。

标签: python selenium web-scraping selenium-chromedriver


【解决方案1】:

使用此示例如何解析带有beautifulsoup 的页面:

from bs4 import BeautifulSoup
import requests


url = 'https://gbr.milesplit.com/athletes/pro/5424599/stats'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

for event, time in zip(soup.select('td.event'),
                       soup.select('td.time')):
    print(event.text, time.text)

打印:

800 Meter Run 1:48.05
1500 Meter Run 3:35.01
800 Meter Run 1:48.87
One Mile Run 3:57.02
8000 Meter Run 24:01.50
10,000 Meter Run 30:43.50
800 Meter Run 1:52.25
One Mile Run 4:05.32
1500 Meter Run 3:43.46
800 Meter Run 1:48.05
1500 Meter Run 3:35.01
800 Meter Run 1:48.87
One Mile Run 3:57.02
8000 Meter Run 24:01.50
10,000 Meter Run 30:43.50
800 Meter Run 1:52.25
One Mile Run 4:05.32
1500 Meter Run 3:43.46

【讨论】:

    猜你喜欢
    • 2018-10-04
    • 2023-03-24
    • 2021-01-23
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 2017-10-06
    相关资源
    最近更新 更多