I’m getting “AttributeError: ‘NoneType’ object has no attribute ‘string'” when I run the following. however, when the same tasks are performed on a block string variable; it works.
Any Ideas as to what I’m doing wrong?
from BeautifulSoup import BeautifulSoup
from urllib import urlopen
url = ("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Albert%20Einstein&explaintext")
print ((BeautifulSoup(((urlopen(url)).read()))).find('extract').string).split("\n", 1)[0]
The find method is not finding anything with the tag ‘extract’. If you want to see it work then give it a HTML tag that exists in the document like ‘pre’ or ‘html’
‘extract’ looks like an xml tag. You might want to try reading the BeautifulSoup documentation on parsing XML – http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html#Parsing XML. Also there is a new version of BeautifulSoup out there (bs4). I find the API much nicer.