【问题标题】:Can't scrape table using lxml无法使用 lxml 刮表
【发布时间】:2019-08-30 12:48:09
【问题描述】:

我正在尝试从该网站上抓取各个玩家的网址。

我已经尝试使用 bs4 执行此操作,并且每次我尝试查找表时它都会返回 []。切换到 lxml 尝试一下。

    import urlopen from urllib.requests
    import lxml.html

    url = "https://www.espn.com/soccer/team/squad/_/id/359/arsenal"

    tree = etree.HTML(urlopen(url).read())

    table = tree.xpath('/* 
 [@id="fittPageContainer"]/div[2]/div[5]/div[1]/div/article/div/section/div[5]/section/table/tbody/tr/td[1]/div/table/tbody/tr[1]/td/span')

    print(table)

我希望可以使用某种排序输出来获取链接,但代码返回方括号

【问题讨论】:

  • 我认为那里的所有数据都是通过 Javascript 作为 JSON 加载的,然后通过 Javascript 映射到 HTML 表。

标签: html python-3.x lxml elementtree


【解决方案1】:

我想这就是你想要的。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Firefox(executable_path=r'C:\files\geckodriver.exe')
driver.set_page_load_timeout(30)

driver.get("https://www.espn.com/soccer/team/squad/_/id/359/arsenal")


continue_link = driver.find_element_by_tag_name('a')
elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    print(elem.get_attribute("href"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 2019-11-18
    • 2021-01-10
    相关资源
    最近更新 更多