【问题标题】:Need help parsing the php/html file using python需要帮助使用 python 解析 php/html 文件
【发布时间】:2019-06-24 13:32:25
【问题描述】:

我想保留 url https://www.horsedeathwatch.com/index.php 并将数据转储到 Pandas 数据框中。

栏如马/日期/路线/死因 我试过 pandas read_html 直接读取这个 url,即使它有 table 标签,它也没有找到 table。

我尝试使用:

  url='https://www.horsedeathwatch.com/index.php'
  #Create a handle, page, to handle the contents of the website
  page = requests.get(url)
  #print(page.text)
  soup = BeautifulSoup(page.content,'lxml')

然后是 findall('tr') 方法,但由于某种原因无法正常工作。

我想做的第二件事是..每匹马(网页表中的第一列)都有一个带有附加属性的超链接。

关于如何将这些附加属性检索到 pandas 数据框的任何建议

【问题讨论】:

    标签: python html parsing


    【解决方案1】:

    查看该站点,我可以看到数据是使用向/loaddata.php 传递页码的 POST 请求加载的。将其与 pandas.read_html 结合:

    import requests
    import pandas
    
    res = requests.post('https://www.horsedeathwatch.com/loaddata.php', data={'page': '3'})
    html = pandas.read_html(res.content)
    

    虽然BeautifulSoup 可能会给您提供更丰富的数据结构.. 因为如果您想针对每匹马提取更多属性,您需要获取锚元素的“href”并执行另一个请求 - 这是一个 GET请求,您需要在响应中解析来自<div class="view"> 的响应内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多