Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BeautifulSoup无法提取元数据_Python_Metadata_Beautifulsoup_Web Scraping - Fatal编程技术网

Python BeautifulSoup无法提取元数据

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

我正在尝试创建一个函数,它将从给定的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方法

文件

您可能希望自己使用Python的urllib2模块获取数据
在将其输入BeautifulSoup或查看类似于scrapy模块的内容之前。

感谢您的回答,我今天才开始学习python,我尝试阅读了BS文档,但没有完全理解它。再次感谢,非常感谢。