BeautifulSoup 无法提取元数据
我正在尝试创建一个函数,该函数将从给定的 URL 中提取元关键字并返回它。然而,无论我传递给它什么 URL,它总是会失败。
def GetKeywords(url):
soup = BeautifulSoup(url)
keywords = soup.findAll('meta', attrs={'name':re.compile("^keywords$", re.I)}) #Find all meta keywords on that page
if len(keywords) == 0: #Check to see if that page has any meta keywords to begin with
print "No meta keywords for: " + str(url)
return -1
else: #If so then return them
return keywords
I am trying to create a function which will extract meta keywords from a given URL and return it. However no matter what URLs I pass to it, it will always fail.
def GetKeywords(url):
soup = BeautifulSoup(url)
keywords = soup.findAll('meta', attrs={'name':re.compile("^keywords$", re.I)}) #Find all meta keywords on that page
if len(keywords) == 0: #Check to see if that page has any meta keywords to begin with
print "No meta keywords for: " + str(url)
return -1
else: #If so then return them
return keywords
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BeautifulSoup 在哪里声明它会接受并获取 URL?
抱歉,请先自己阅读 BeautifulSoup 文档,而不是尝试和猜测 API 方法。
http://www.crummy.com/software/BeautifulSoup/documentation.html#Parsing a Document
你想要的可能是使用Python的urllib2模块来获取数据你自己
在将其输入 BeautifulSoup 或查看类似 scrapy 模块的内容之前。
Where does the BeautifulSoup state that it would accept and fetch an URL?
Sorry but read the BeautifulSoup documentation first yourself instead trying and guessing API methods..
http://www.crummy.com/software/BeautifulSoup/documentation.html#Parsing a Document
What you want is likely using the urllib2 module of Python for fetching data yourself
before feeding it into BeautifulSoup or you look at something like the scrapy module.