【问题标题】:Remove tags from beautiful soup extract从美丽的汤提取物中删除标签
【发布时间】:2018-05-30 12:02:00
【问题描述】:

我是网络抓取的新手,并试图弄清楚如何删除不需要的标签。

我想从加拿大银行网站获取有关货币政策的公告和相应日期。我的代码如下:-

from bs4 import BeautifulSoup
import urllib
r=urllib.request.urlopen('https://www.bankofcanada.ca/content_type/publications/mpr/?post_type%5B0%5D=post&post_type%5B1%5D=page').read()
soup = BeautifulSoup(r)

soup.prettify()
letters = soup.find_all("div", class_="media-body")
lobbying = {}
for element in letters:
    lobbying[element.a.get_text()] = {}
print(lobbying)

屏幕截图中附有输出。 enter image description here

预期输出:-

2017 年 4 月 12 日:预计加拿大经济今年将增长 2.5%,2018 年和 2019 年将略低于 2%

2016 年 4 月 13 日:加拿大经济预计在 2016 年增长 1.7%,随着复杂调整的继续,明年将恢复潜力

提前致谢

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    您希望在每个 media div 中添加 media-datemedia-excerpt 标签,然后去掉空格:

    from bs4 import BeautifulSoup
    import urllib.request
    
    r = urllib.request.urlopen(
        'https://www.bankofcanada.ca/content_type/publications/mpr/?post_type%5B0%5D=post&post_type%5B1%5D=page').read()
    soup = BeautifulSoup(r, "lxml")
    
    lobbying = {}
    
    # All media/div elements.
    for element in soup.select(".media"):
        # select_one pulls 1 match, pull the text from each tag.
        lobbying[element.select_one(".media-date").text] = element.select_one(".media-excerpt").text.strip()
    print(lobbying)
    

    这会给你:

       {
        'April 18, 2018': 'The Bank’s new forecast calls for economic growth of 2.0 percent this year, 2.1 per cent in 2019 and 1.8 per cent in 2020.',
        'January 17, 2018': 'Growth in the Canadian economy is projected to slow from 3 per cent in 2017 to 2.2 per cent this year and 1.6 per cent in 2019.',
        'October 25, 2017': 'Projections for Canadian economic growth have been increased to 3.1 per cent this year and 2.1 per cent in 2018, with growth of 1.5 per cent forecast for 2019.',
        'July 12, 2017': 'Growth in the Canadian economy is projected to reach 2.8 per cent this year before slowing to 2.0 per cent next year and 1.6 per cent in 2019.',
        'April 12, 2017': 'Canada’s economy is expected to grow by 2 1/2 per cent this year and just below 2 per cent in 2018 and 2019.',
        'January 18, 2017': 'The Canadian economy is expected to expand by 2.1 per cent this year and in 2018.',
        'October 19, 2016': 'Growth in the Canadian economy is expected to increase from 1.1 per cent this year to about 2.0 per cent in 2017 and 2018.',
        'July 13, 2016': 'Canadian economic growth is projected to accelerate from 1.3 per cent this year to 2.2 per cent in 2017.',
        'April 13, 2016': 'Canada’s economy is projected to grow by 1.7 per cent in 2016 and return to potential next year as complex adjustments continue.',
        'January 20, 2016': 'Growth in Canada’s economy is expected to reach 1.4 per cent this year and accelerate to 2.4 per cent in 2017.'}
    

    您也可以只使用 dict 理解 来创建 dict:

    lobbying = {el.select_one(".media-date").text: el.select_one(".media-excerpt").text.strip()
                for el in soup.select(".media")}
    

    【讨论】:

      猜你喜欢
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 2021-02-08
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      相关资源
      最近更新 更多