【问题标题】:beautifulsoup with an invalid html document带有无效 html 文档的 beautifulsoup
【发布时间】:2014-06-14 08:53:45
【问题描述】:

我正在尝试解析文档http://www.consilium.europa.eu/uedocs/cms_data/docs/pressdata/en/ecofin/5923en8.htm。 我想提取Commission: 之前的所有内容。

我需要 Beautifulsoup,因为第二步是提取国家和人名

如果我这样做:

import urllib
import re
from bs4 import BeautifulSoup
url="http://www.consilium.europa.eu/uedocs/cms_data/docs/pressdata/en/ecofin/5923en8.htm"
soup=BeautifulSoup(urllib.urlopen(url))
print soup.find_all(text=re.compile("Commission"))

我得到的唯一结果是:

[u'The Governments of the Member States and the European Commission were represented as follows:']

这是单词的第一次出现,但不是我要查找的行。我认为这是因为该文件无效,但不确定。如果我查看源代码:

<B><U><P>Commission</B></U>:</P>

但如果我打印soup,我可以看到文本,标签重新排序:

<u><b>Commission</b></u>

我怎样才能得到这个元素"Commission:"

我正在使用 python 2.7 和 Beautifoulsoup 4.3.2。


编辑:已解决!

按照alecxe的建议,我换了一行:

soup=BeautifulSoup(urllib.urlopen(url))

BeautifulSoup(urllib.urlopen(url), 'html.parser')

它现在可以工作了:)。 谢谢大家。


编辑:类似问题

我用相同的解决方案发现了类似的问题:

Beautiful Soup 4 find_all don't find links that Beautiful Soup 3 finds

Beautiful Soup findAll doen't find them all

【问题讨论】:

  • 但是运行这个脚本我得到了总共 9 个字符串:第二个就是这里提到的那个。
  • 好吧,我们没有得到相同的结果。是因为我的python/Beautifoulsoup版本吗?
  • 我运行了您帖子中所写的内容,bs4 和 python 2.7。

标签: python html parsing html-parsing beautifulsoup


【解决方案1】:

如果您希望标签之前的所有内容都带有“佣金:”值。你可以在没有 beatifulsoup 的情况下做到这一点......只需将其视为字符串变量并搜索正确的关键字并删除字符串的其余部分。

但是当我运行你的代码时,我得到以下信息:

[u'The Governments of the Member States and the European Commission were represe
nted as follows:', u'Commission', u'The Council held an orientation debate on ke
y economic policy issues with a view to giving guidance to the Commission on the
 questions Ministers wish to be addressed in the broad economic policy guideline
s 1998/99 for which the Commission will present its recommandation later in the
Spring. It was noted that the forthcoming guidelines are of particular importanc
e given the start of stage 3 of EMU.', u'The debate was based on an assessment o
f the economic situation and outlook in the Community carried out by the Commiss
ion and the Economic Policy and Monetary Committees.', u"The Council held an ori
entation debate on the Commission's Communication setting out a possible Communi
ty framework allowing Member States to experiment with reduced VAT rates for lab
our-intensive services in order to boost employment in small businesses without
distorting international competition. ", u'This Communication was tabled by the
Commission as a follow-up to the Employment European Council of last November in
 Luxembourg, which concluded that, in order to make the taxation system more emp
loyment-friendly, "Member States will examine, without obligation, the advisabil
ity of reducing the rate of VAT on labour-intensive services not exposed to cros
s-border competition".', u"In conclusion, the Council invited Coreper to examine
 the technical questions arising from today's debate and to report back to it wi
th a view to deciding on a possible request to the Commission to submit a propos
al in this area. ", u"This technical examination should be carried out, taking i
nto account the criteria indicated in the Commission's Communication for a reduc
ed VAT rate, on the following questions :", u'An initial trial period running un
til the year 2002 should identify the best method for allocating FISIM. At the e
nd of this period, the Commission will assess the results of the trial period an
d decide, by means of a comitology procedure, on the final methodology to be app
lied. However, a unanimous decision by the Council would be needed in order to u
se the new methodology in budgetary calculations on other Community policies and
 notably concerning "own resources".']

【讨论】:

  • 这不是一个好的解决方案,因为我想处理提取的html(我想提取国家和人名)。
  • 您没有说明该标准。
  • 您写道“我想在委员会之前提取所有内容:。”我刚刚为您提供了该声明的可行解决方案。
  • 好吧抱歉,我刚刚在我的第一篇文章中添加了它,我的错误(我没有对你投反对票)。
  • 好吧,我们没有得到相同的结果。是不是因为我的python/Beautifoulsoup版本?
【解决方案2】:

遍历p元素并在找到以Commission开头的文本时停止:

import urllib
from bs4 import BeautifulSoup

url="http://www.consilium.europa.eu/uedocs/cms_data/docs/pressdata/en/ecofin/5923en8.htm"
soup=BeautifulSoup(urllib.urlopen(url))

for item in soup.find_all('p'):
    if item.text.startswith('Commission'):
        break
    else:
        print item.text

它打印Commission之前的所有内容:

The Governments of the Member States and the European Commission were represented as follows:
Belgium:
...
Ms Helen LIDDELL            Economic Secretary to the Treasury
* * *

【讨论】:

  • 正在尝试运行您的代码...我有错误:File "/usr/local/lib/python2.7/dist-packages/bs4/element.py", line 1198, in descendants current = current.next_element AttributeError: 'NoneType' object has no attribute 'next_element'
  • @rom 你安装了html5lib 吗?如果没有,请尝试安装它并重新运行代码。另外,尝试将soup=BeautifulSoup(urllib.urlopen(url)) 替换为soup=BeautifulSoup(urllib.urlopen(url), 'html.parser')
  • 它有效:)。我刚刚按照您的建议添加了“html.parser”,现在我得到了与每个人相同的结果:D。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多