【问题标题】:BeautifulSoup Cannot Extract MetadataBeautifulSoup 无法提取元数据
【发布时间】:2011-08-30 13:06:32
【问题描述】:

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

【问题讨论】:

    标签: python metadata beautifulsoup web-scraping


    【解决方案1】:

    BeautifulSoup 在哪里声明它将接受获取 URL?

    soup = BeautifulSoup(url)
    

    抱歉,请自己阅读 BeautifulSoup 文档首先,而不是尝试和猜测 API 方法..

    http://www.crummy.com/software/BeautifulSoup/documentation.html#Parsing 一个文档

    你想要的可能是使用 Python 的 urllib2 模块来获取数据你自己 在将其输入 BeautifulSoup 之前,或者您查看类似 scrapy 模块的内容。

    【讨论】:

    • 感谢您的回答,我今天刚开始学习python,我尝试阅读BS文档但不太明白。再次感谢,非常感谢。
    猜你喜欢
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2017-11-10
    • 2018-07-31
    相关资源
    最近更新 更多