我正试图从一个选举网站上搜集一些数据,但不知道如何使用BeautifulSoup提取这些数据
我尝试过的代码
import pandas as pd
from bs4 import BeautifulSoup
tx_url = 'https://results.texas-election.com/contestdetails?officeID=1001&officeName=PRESIDENT%2FVICE-PRESIDENT&officeType=FEDERAL%20OFFICES&from=race'
import urllib.request
local_filename, headers = urllib.request.urlretrieve(tx_url)
urllib.error.HTTPError: HTTP Error 403: Forbidden
soup = BeautifulSoup(tx_url)
/home/server/pi/homes/woodilla/.conda/envs/baseDS_env/lib/python3.7/site-packages/bs4/init.py:357: UserWarning: "https://results.texas-election.com/contestdetails?officeID=1001&officeName=PRESIDENT%2FVICE-PRESIDENT&officeType=FEDERAL%20OFFICES&from=race" looks like a URL. Beautiful Soup is not an HTTP client. You should probably use an HTTP client like requests to get the document behind the URL, and feed that document to Beautiful Soup. that document to Beautiful Soup.' % decoded_markup
下面是该表的外观:
首先,您得到的错误意味着您错误地使用了
BeautifulSoup
您需要将HTTP客户端的响应传递给
BeautifulSoup
,如下所示:其次,你不需要
BeautifulSoup
去刮那一页。一切都会回来的。例如:输出:
编辑:
要获取您提到的特定URL的数据,只需使用以下命令:
注意:这只是数据的一小部分,因为
JSON
是巨大的。我添加了用于转储整个JSON
的代码,这样您就可以按照您想要的方式解析它样本输出:
我是如何找到
JSON
的相关问题 更多 >
编程相关推荐