当我运行以下命令时,我得到"AttributeError:'NoneType‘object has no attribute 'string'“。但是,当在块字符串变量上执行相同的任务时,它会起作用。
知道我做错了什么吗?
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]发布于 2012-05-16 20:21:51
from BeautifulSoup import BeautifulSoup
from urllib import urlopen
url = ("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Albert%20Einstein&explaintext")
soup = BeautifulSoup(urlopen(url).read())
print soup.find('extract') # returns Nonefind方法找不到具有'extract‘标记的任何内容。如果你想看到它工作,那么给它一个HTML标签,这个标签存在于文档中,比如'pre‘或'html’
‘'extract’看起来像一个xml标签。您可能希望阅读有关解析XML - http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html#Parsing XML的BeautifulSoup文档。此外,还有一个新版本的BeautifulSoup (bs4)。我发现API要好得多。
https://stackoverflow.com/questions/10618144
复制相似问题