【问题标题】:Get links in Beautiful Soup获取美丽汤中的链接
【发布时间】:2017-07-13 03:18:55
【问题描述】:

我正在尝试解析 Beautiful Soup 中的以下链接,但我不确定这样做的最佳方法是什么。任何建议将不胜感激。

谢谢

【问题讨论】:

    标签: beautifulsoup


    【解决方案1】:

    如果有人有兴趣,我想出了如何做到这一点:

     from bs4 import BeautifulSoup
    xml = requests.get("http://www.realclearpolitics.com/epolls/2010/governor/2010_elections_governor_map.html").text
    def find_governor_races(html):
        soup = BeautifulSoup(html, 'html.parser')
        pattern = "http://www.realclearpolitics.com/epolls/????/governor/??/*-*.html"
    
        links = []
    
        for option in soup.find_all('option'):
            links.append(option['value'])
    
        matched_links = []
    
        for link in links:
            if fnmatch(link, pattern):
                matched_links.append(link)
    
        return matched_links
    

    【讨论】:

      猜你喜欢
      • 2015-08-01
      • 2021-09-08
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多