我正在尝试创建一个函数,该函数将从给定的 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
最佳答案
BeautifulSoup 在哪里声明它会接受并获取 URL?
soup = BeautifulSoup(url)
抱歉,请先自己阅读 BeautifulSoup 文档,而不是尝试和猜测 API 方法。
http://www.crummy.com/software/BeautifulSoup/documentation.html#Parsing一个文档
您想要的可能是使用 Python 的 urllib2 模块来获取数据您自己 在将其输入 BeautifulSoup 之前,或者查看类似 scrapy 模块的内容。
关于python - BeautifulSoup 无法提取元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6087291/