Python BeautifulSoup无法提取元数据
我正在尝试创建一个函数,它将从给定的URL提取元关键字并返回它。然而,无论我传递给它什么URL,它总是会失败Python BeautifulSoup无法提取元数据,python,metadata,beautifulsoup,web-scraping,Python,Metadata,Beautifulsoup,Web Scraping,我正在尝试创建一个函数,它将从给定的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: #C
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方法
文件
您可能希望自己使用Python的urllib2模块获取数据
在将其输入BeautifulSoup或查看类似于scrapy模块的内容之前。感谢您的回答,我今天才开始学习python,我尝试阅读了BS文档,但没有完全理解它。再次感谢,非常感谢。