Ich versuche sechs Wikipedia-Seiten zu durchlaufen, um eine Liste aller verknüpften Songs zu erhalten. Es gibt mir diesen Fehler, wenn ich es im Terminal laufen:Warum bekomme ich eine "None" in meinem Python-Code?
Traceback (most recent call last):
File "scrapeproject.py", line 31, in <module>
print (getTableLinks(my_url))
File "scrapeproject.py", line 20, in getTableLinks
html = urlopen(my_url)
File "/Users/adriana/Software/Python-3.5.1/mybuild/lib/python3.5/urllib/request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "/Users/adriana/Software/Python-3.5.1/mybuild/lib/python3.5/urllib/request.py", line 456, in open
req.timeout = timeout
AttributeError: 'NoneType' object has no attribute 'timeout'
Ich denke, das liegt daran, dass eine None zeigt sich hält, wenn ich die Songliste drucken. Hat jemand irgendwelche Vorschläge?
Code:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import sys
import http.client
main = "https://en.wikipedia.org/wiki/Billboard_Year-End_Hot_100_singles_of_"
year = 2009
def createUrl(main, year):
for i in range(0, 6): # increment years so i can get each link
year += 1
print ("\n\n", year, "\n\n")
fullUrl = main + str(year)
return fullUrl
my_url = createUrl(main, year) # this is how i make createUrl a variable to be used in other functions
def getTableLinks(my_url): # there is a random none appearing in my code
# i think the problem is between here...
html = urlopen(my_url)
bsObj = BeautifulSoup(html.read(), "html.parser")
tabledata = bsObj.find("table", {"class":"wikitable"}).find_all("tr")
# ...and here
for table in tabledata:
try:
links = table.find("a")
if 'href' in links.attrs:
print (links.attrs['href'])
except:
pass
print (getTableLinks(my_url))
Vielen Dank! Ich bekomme immer noch ein merkwürdiges Nichts, aber es schlingt sich schließlich durch meinen Code und bekommt alle Links, die ich brauche. –
Gut. Fühlen Sie sich frei zu upvote. – Chris
Würde gerne ... aber ich bin so neu, ich habe nicht das Privileg zu lol –