【问题标题】:Beautifulsoup parsing html line breaksBeautifulsoup 解析 html 换行符
【发布时间】:2018-01-16 19:19:21
【问题描述】:

我正在使用 BeautifulSoup 从文本文件中解析一些 HTML。文本被写入字典,如下所示:

websites = ["1"]

html_dict = {}

for website_id in websites:
    with codecs.open("{}/final_output/raw_html.txt".format(website_id), encoding="utf8") as out:   
        get_raw_html = out.read().splitlines()
        html_dict.update({website_id:get_raw_html})

我从 html_dict = {} 解析 HTML 以查找带有 <p> 标签的文本:

scraped = {}

for website_id in html_dict.keys():
    scraped[website_id] = []
    raw_html = html_dict[website_id]
    for i in raw_html:
        soup = BeautifulSoup(i, 'html.parser')
        scrape_selected_tags = soup.find_all('p')

这是html_dict 中的 HTML 的样子:

<p>Hey, this should be scraped
but this part gets ignored for some reason.</p>

问题是,BeautifulSoup 似乎正在考虑换行并忽略第二行。所以当我打印出scrape_selected_tags 时,输出是......

<p>Hey, this should be scraped</p>

当我期待全文时。

我怎样才能避免这种情况?我试过在html_dict 中拆分行,但它似乎不起作用。提前致谢。

【问题讨论】:

  • 您是否尝试过其他解析器,例如 lxml?
  • 你也可以去掉BeautifulSoup的第二个参数,它会自动推荐你系统上最好的解析器。
  • @ForceBru 这是推荐的可用解析器。
  • 删除splitlines?
  • @t.m.adam 当我这样做时,我收到此错误UserWarning: "." looks like a filename, not markup. You should probably open this file and pass the filehandle into Beautiful Soup.

标签: python beautifulsoup


【解决方案1】:

通过在阅读 html 文档时调用 splitlines,您会破坏字符串列表中的标签。
相反,您应该阅读字符串中的所有 html。

websites = ["1"]
html_dict = {}

for website_id in websites:
    with codecs.open("{}/final_output/raw_html.txt".format(website_id), encoding="utf8") as out:   
        get_raw_html = out.read()
        html_dict.update({website_id:get_raw_html})

然后删除内部 for 循环,这样您就不会遍历该字符串。

scraped = {}

for website_id in html_dict.keys():
    scraped[website_id] = []
    raw_html = html_dict[website_id]
    soup = BeautifulSoup(raw_html, 'html.parser')
    scrape_selected_tags = soup.find_all('p')

BeautifulSoup 可以处理标签内的换行符,我举个例子:

html = '''<p>Hey, this should be scraped
but this part gets ignored for some reason.</p>'''

soup = BeautifulSoup(html, 'html.parser')
print(soup.find_all('p'))

[&lt;p&gt;Hey, this should be scraped\nbut this part gets ignored for some reason.&lt;/p&gt;]

但如果你将一个标签拆分为多个BeautifulSoup 对象:

html = '''<p>Hey, this should be scraped
but this part gets ignored for some reason.</p>'''

for line in html.splitlines():
    soup = BeautifulSoup(line, 'html.parser')
    print(soup.find_all('p'))

[&lt;p&gt;Hey, this should be scraped&lt;/p&gt;]
[]

【讨论】:

  • 你的方法只给了我文件中的 8 个字,而我的给了 203。我不认为分割线有什么不同。这是 HTML 在文本文件中的方式。
  • 当然有,这是一个完全不同的文档(除非你的文件内容没有换行符)
  • 您无法理解的是,两者之间存在很大差异。例如,如果我们有一个文本 = '你好,我的世界,我的名字是傻瓜'。如果我们说 `print(text)' 它会打印整个文本,如果我们拆分它并寻找一部分,在情况 1. 它将查看整个字符串 2. 它只会查看一部分的一部分(因为我们拆分它)。谢谢你,亚当,你的服务很棒。希望我们所有人都能从我们得到的回应以及这些专家无偿为我们付出的时间中反思、思考和研究。
  • 感谢@ElvirMuslic,但我这样做不是为了“名声”(当然不是为了钱!),我也不指望感激。我只是相信知识共享,我有一些空闲时间,仅此而已。
  • @t.m.adam 你是一个体面的人,应该受到尊重。你和这个社区的许多人一样,已经付出了努力和时间来帮助人们。您是您所在领域的专家,您可以做任何其他事情,但您选择帮助我们。这是一个非常周到的行为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
  • 2014-03-06
  • 2011-07-21
  • 2012-12-13
  • 2011-09-24
  • 2018-07-10
  • 2021-10-31
相关资源
最近更新 更多