【问题标题】:Regular expression to clean webscraped text正则表达式清理网页抓取的文本
【发布时间】:2019-11-28 17:14:06
【问题描述】:

我正在尝试为维基百科页面提取一些信息,并且我正在使用 Beautiful soup 将文本加载到 Python 中,但我似乎无法使用正则表达式去除所有不必要的标签。

这是来自美丽汤的文本输出示例

[<td colspan="3">
</td>, <td valign="top" width="400">
<ul><li><a href="/wiki/Aach,_Baden-W%C3%BCrttemberg" title="Aach, Baden-Württemberg">Aach</a> (<a href="/wiki/Baden-W%C3%BCrttemberg" title="Baden-Württemberg">Baden-Württemberg</a>)</li>
<li><a href="/wiki/Aachen" title="Aachen">Aachen</a> (<a href="/wiki/North_Rhine-Westphalia" title="North Rhine-Westphalia">North Rhine-Westphalia</a>)</li>

理想情况下,我希望拥有城市(分配给标题)和区域(就在行尾之前)。

任何帮助将不胜感激!

rows = soup.find_all('td')
list_rows = []

#remove html tags
for row in rows:
    cells = row.find_all('li')
    str_cells = str(cells)
    clean = re.compile('<.*?>')
    clean2 = (re.sub(clean, '', str_cells))
    list_rows.append(clean2)
print(clean2)

【问题讨论】:

  • 可以分享一下维基百科页面的网址吗?
  • 结束锚标记 (&lt;/a&gt;) 之前的文本也是区域吗?
  • 在某些情况下是的,其中区域是超链接,但在其他情况下,区域就在结束锚标记之前 ()

标签: python regex python-3.x beautifulsoup


【解决方案1】:

这种情况下可以使用.find_next_sibling()方法:

import re
import requests
from bs4 import BeautifulSoup

url='https://en.wikipedia.org/wiki/List_of_cities_and_towns_in_Germany'

soup = BeautifulSoup(requests.get(url).text, 'lxml')

for li in soup.select('td[width="400"] li'):
    city = li.select_one('a')
    if city.find_next_sibling('a'):
        region = city.find_next_sibling('a').text
    else:
        region = city.find_next_sibling(text=True).strip()
    print('{: <30}{}'.format(city.text, re.findall(r'[^()]+', region)[0]))

打印:

Aach                          Baden-Württemberg
Aachen                        North Rhine-Westphalia
Aalen                         Baden-Württemberg
Abenberg                      Bavaria
Abensberg                     Bavaria
Achern                        Baden-Württemberg
Achim                         Lower Saxony
Adelsheim                     Baden-Württemberg
Adenau                        Rhineland-Palatinate
Adorf                         Saxony
Ahaus                         North Rhine-Westphalia
Ahlen                         North Rhine-Westphalia
Ahrensburg                    Schleswig-Holstein
Aichach                       Bavaria
Aichtal                       Baden-Württemberg
Aken (Elbe)                   Saxony-Anhalt
Albstadt                      Baden-Württemberg
Alfeld                        Lower Saxony
Allendorf (Lumda)             Hesse
Allstedt                      Saxony-Anhalt

...and so on.

【讨论】:

  • 谢谢 Andrej,出于好奇,是使用 Beautiful soup 清理网页抓取的文本还是使用正则表达式更好?
  • @JayDoe 通过 BeautifulSoup 更好地解析 HTML。在大多数情况下,通过正则表达式解析 HTML 绝不是一个好主意。
  • 确认一下,'select'、'select_one'和'find_next_sibling'方法都是Beautiful Soup方法?谢谢杰
  • @JayDoe 是的,它们是 BeautifulSoup 的方法。文档在这里:crummy.com/software/BeautifulSoup/bs4/doc确保您使用的是最新版本!
【解决方案2】:

这里有两个正则表达式可以做你想做的事:

这个正则表达式似乎可以为您提供所有这些城镇名称标题属性,尽管如果城镇名称中有不同的特殊字符,它可能需要一些调整。这将捕获空格、破折号和逗号。 title=\"([\w ,-]+)\"&gt;[\w]+&lt;/a&gt;[^\)]

你可以测试一下here

这应该会为您提供第一个捕获组中的区域名称,尽管适用于特殊字符的相同警告: ([\w ,-]+)(&lt;/a&gt;)?\)&lt;/li&gt;

你可以测试一下here

【讨论】:

  • 感谢您的链接 - 我会将其用作学习资源并使用正则表达式。
猜你喜欢
  • 2020-09-28
  • 1970-01-01
  • 2012-07-14
  • 2014-10-23
  • 2017-02-02
  • 2014-02-01
  • 2012-04-10
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多