0

I am using the code below to try an extract the data from the table in this URL. I asked the same question here and got an Answer for it. However, despite the code from the Answer working at that time I've now come to realize that data in the code hasn't been defined and hence the code below results in the error AttributeError: 'NoneType' object has no attribute 'text' in the line final = [[t.th.text] + [ele.text for ele in t.find_all("td")] for t in h[-1].find_all_next("tr")]. I have asked the user what data is, but I haven't been able to figure it out myself and was therefore hoping for some help in this regard.

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

1 Answer 1

1
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]
Sign up to request clarification or add additional context in comments.

4 Comments

Thank You. However, I still get the Error AttributeError: 'NoneType' object has no attribute 'text' in the line final = [[t.th.text] + [ele.text for ele in t.find_all("td")] for t in h[-1].find_all_next("tr"). Do you know why this might be
Are you sure you copied it properly? I just tried it and its working perfectly fine.
Yes, its exactly the same. Do you think the version of beautifulsoup or Python could be having an impact?
Try using htmllib. First install it as pip install html5lib. Then use it to generate the soup - soup = BeautifulSoup(r.content, 'html5lib')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.