【问题标题】:BeautifulSoup parse returning empty setBeautifulSoup 解析返回空集
【发布时间】:2013-11-01 15:40:29
【问题描述】:

更新代码就在下面)

我有一个类:UrlData,它会生成一个 url 列表:

for url in urls:
    rawMechSiteInfo = mech.open(url)  #mechanize browse each url
    mech_response = mech.response()
    headers = mech_response.info()
    print "headers ", headers.getheader('Content-Type').encode('utf-8')
return

这一行:print "headers ", headers.getheader('Content-Type').encode('utf-8')

什么都不输出

如果我在我的view 中执行print url_data.url_list(),它会抛出:

<Scan.urldata.UrlData object at 0x103e73f50>

我只是想解析一个 html 标记的 src,例如:

<div class="s">
   <div>
      <div class="f kv" style="white-space:nowrap">
         <cite class="vurls">www.somewebsite.com/</cite>‎
      </div>
   </div>
</div>

以下正在工作解析整个文档:

HarvestLinks = 'h3',attrs={'class': 'r'}

HarvestLinks = BSObjOfUrl.find('cite','vurls')

它似乎工作正常,但最后只返回一个结果并且有多个

感谢迄今为止尝试提供帮助的人,还有其他想法吗?

【问题讨论】:

  • 尝试只搜索自定义标签,以了解是否检测到它们:HarvestLinks = BSObjOfUrl.find_all('cite')
  • 看来他们不是,如果它是任何非自定义标签。例如

    等,它会很简单

标签: python django parsing beautifulsoup mechanize


【解决方案1】:

检查 headers 变量的输出并报告,您似乎仍然有错误的编码:

def url_list(self):
    #setup mechanize
    ###
    ### Mechanize settings are here.
    ###

    for url in urls:
        rawMechSiteInfo = mech.open(url)  #mechanize browse each url
        mech_response = mech.response()
        headers = mech_response.info()
        print "headers ", headers.getheader('Content-Type')
        #results = unicode(mech_response.read()) 
        #BSObjOfUrl = BeautifulSoup(results)
        #HarvestLinks = BSObjOfUrl.find_all(u'cite', class_='vurls')
    #return HarvestLinks
    return

【讨论】:

  • 感谢您的回答,我尝试将其实现到我的代码中(以上内容现已更新),但它似乎没有输出正确的值,而只是 [] [] [] [] [] [] [] [] [] [](因为我们已经以前见过)。关于为什么这不起作用的任何进一步的想法?
  • 能否请您提供指向源 html 的链接或将它的源代码放在某个地方。似乎问题出在编码上,但我需要查看来源以了解问题所在。
  • 顺便说一句,你的答案是: print "headers ", headers."Content-type" 抛出一个无效语法错误
  • 请查看我输入的更新代码和我收到的错误:'ascii' codec can't decode byte 0xe2 in position 209176: ordinal not in range(128)
  • headers["Content-type"] 替换headers.["Content-type"] 并报告headers 值。
【解决方案2】:

查看文档,attrs 是一个设计不佳的参数,应该更像是一个 **kwargs。

http://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-by-css-class表示你确实想通过class_kwarg:

>>> from bs4 import BeautifulSoup
>>> src = """ <div class="s">
...    <div>
...       <div class="f kv" style="white-space:nowrap">
...          <cite class="vurls">www.somewebsite.com/</cite>\U+200E
...       </div>
...    </div>
... </div>
...
... """
>>> soup = BeautifulSoup(src)
>>> soup.find_all('cite')
[<cite class="vurls">www.somewebsite.com/</cite>]
>>> soup.find_all('cite', attr={'class': 'vurls'})
[]
>>> soup.find_all('cite', class_='vurls')
[<cite class="vurls">www.somewebsite.com/</cite>]

【讨论】:

  • 请查看上面 for() 循环的更新代码。这没有输出任何东西
  • _class 仅在 BeautifulSoup >= 4.1.x 中可用
  • 使用的是什么版本?当前似乎是4.3.2。旧版 3.2.1 与 soup.findAll('cite', {'class': 'vurls'}) 完美配合。但是,使用 find_all 而不是 findAll 表示 4.0?你能升级你的图书馆吗?
  • 我已经在使用版本 4 并且我尝试了这两个示例,但似乎都不起作用。正如评论者 nickzam 所说,它认为“引用”属性正在抛弃它
  • 通过使用:class_='vurls' 它输出:[] [] [] [] [] [] [] [] [] []
【解决方案3】:

我以前从未使用过mechanize,我一直在使用urllib2 和beautifulsoup4。 我多次遇到编码和解码问题。也许我的一些经验会有所帮助。

当您从页面读取文本时,elem.text,默认始终是 unicode。有时人们有好运直接将 unicode 打印到屏幕上,一切都很好。有时,控制台不会正确显示 unicode。这表明两件事:

  1. 你已经准备好里面的数据了,唯一的问题是你想在IDE(Eclipse,Pycharm,..etc)中看到它是行不通的。您可以在不执行任何操作的情况下将 unicode 写入数据库或文件,有时当您在 IDE 之外看到数据时,它会正确显示。

  2. 如果您想在编写代码时首先看到文本(谁不想?)您可以print elem.text.encode('utf-8'),我总是很幸运。

【讨论】:

  • 如何将您的第 2 点与我的代码结合起来?我会很感激的。
  • @CodeTalk 你提到你的打印有问题,也许试试print "headers ", headers.getheader('Content-Type').encode('utf-8') ?
  • 我用上面的代码试过这个,它更新了。它没有输出任何东西
  • @CodeTalk 嗯,什么都不输出?它至少应该打印“标题”,......等等,对吗?
  • 它没有在可见页面上输出任何东西
猜你喜欢
  • 1970-01-01
  • 2014-01-30
  • 2015-11-02
  • 2020-07-15
  • 1970-01-01
  • 2019-07-22
  • 2017-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多