我正在尝试抓取 table 的内容.我相信表格是用 JavaScript 呈现的,所以我使用的是 selenium
包和 Python3
。要完成这样的任务,我见过 others找到表 xpath
以抓取其内容,但我不确定如何识别正确的 xpath。
如何提取表格内容?如果使用 xpath,我如何通过检查网页的源代码来识别与表或其内容对应的正确 xpath?
from selenium import webdriver
driver = webdriver.Chrome('path/to/chromedriver.exe')
url = https://ultrasignup.com/results_event.aspx?did=6727
driver.get(url)
# Now I need to get the tables contents. I might do something like this:
table = driver.find_elements_by_xpath('my_xpath')
table_html = table.get_attribute('innerHTML') # not sure what innerHTML is...
df = read_html(table_html)[0]
print(df)
driver.close()
最佳答案
我相信没有必要去抓取,因为他们有 API。
如果您访问此链接,您将看到来自您提供的表格的格式良好的数据:https://ultrasignup.com/service/events.svc/results/6727/json
部分代码:
import json, requests
url = 'https://ultrasignup.com/service/events.svc/results/6727/json'
response = requests.get(url)
# Get all people from the table
people = [x for x in response.json()]
# Print first person's information
print(people[0])
希望对您有所帮助!
关于javascript - 使用 Python/selenium 抓取网页内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56726544/