通过 Python 从 URL 抓取数据时出现 AttributeError
AttributeError when scraping data from URL via Python
我正在使用下面的代码尝试从 this URL. I asked the same question 中的 table 中提取数据并得到一个答案。但是,尽管当时答案中的代码有效,但我现在意识到代码中的 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")]
。我问过用户 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]
我正在使用下面的代码尝试从 this URL. I asked the same question 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")]
。我问过用户 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]