[英]AttributeError when scraping data from URL via Python
我正在使用下面的代码尝试从此URL中的表中提取数据。 我在这里问了同样的问题,并得到了答案。 但是,尽管当时Answer的代码在起作用,但我现在已经意识到代码中的data
尚未定义,因此下面的代码会导致错误AttributeError: 'NoneType' object has no attribute 'text'
在final = [[t.th.text] + [ele.text for ele in t.find_all("td")] for t in h[-1].find_all_next("tr")]
行final = [[t.th.text] + [ele.text for ele in t.find_all("td")] for t in h[-1].find_all_next("tr")]
。 我问过用户什么data
,但是我自己却无法弄清楚,因此希望在这方面有所帮助。
from bs4 import BeautifulSoup
import requests
from itertools import islice
import pandas as pd
r = requests.get(
"http://www.federalreserve.gov/econresdata/researchdata/feds200628_1.html")
soup = BeautifulSoup(r.content)
h = data.find_all("th", scope="col") #Issue
final = [[t.th.text] + [ele.text for ele in t.find_all("td")] for t in h[-1].find_all_next("tr")]
headers = [th.text for th in h]
headers.insert(0,"dates")
df = pd.DataFrame(final,columns=headers)
print df
from bs4 import BeautifulSoup
import requests
from itertools import islice
r = requests.get(
"http://www.federalreserve.gov/econresdata/researchdata/feds200628_1.html")
soup = BeautifulSoup(r.content)
data = soup.find('table', attrs={'rules': 'all'})
h = data.find_all("th", scope="col") #Issue
final = [[t.th.text] + [ele.text for ele in t.find_all("td")] for t in h[-1].find_all_next("tr")]
headers = [th.text for th in h]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.