【问题标题】:How To Scrape A TableIn Python如何在 Python 中抓取表格
【发布时间】:2017-06-20 07:56:24
【问题描述】:

我刚开始使用 Python。试图从...刮桌子 http://www.dramexchange.com/

但还没有成功。

import requests, bs4
r = requests.get('http://www.dramexchange.com/')
soup = bs4.BeautifulSoup(r.text, 'lxml') 
for table in contents.find_all(class_='tb_NationalDramSpotPrice'):
title = table.find(class_='title').text
for tr in table.find_all('tr'):
    _, Item, Daily High, Daily Low. Session High. Session Low. Session Average, Session Change = [td for td in tr.stripped_strings]
    print(Item, Daily High, Daily Low. Session High. Session Low. Session Average, Session Change)

有什么帮助吗?

【问题讨论】:

  • 您遇到了什么问题?

标签: python web-scraping html-table beautifulsoup


【解决方案1】:

首先看起来你实际上并没有在你的第一个迭代器中引用正确的变量:

soup = bs4.BeautifulSoup(r.text, 'lxml') 
for table in contents.find_all(class_='tb_NationalDramSpotPrice'):

请注意,您是在变量 contents 上调用 find_all(),而不是在之前声明的 soup 上。

我采取了额外的步骤来实际提取您要抓取的网站,它实际上不包含“tb_NationalDramSpotPrice”类,而是该名称的id。这会增加额外的问题。

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2021-01-04
    • 2020-04-02
    • 1970-01-01
    相关资源
    最近更新 更多