【问题标题】:How to iterate through a list and extract text between quotation marks using Python 2.7.10如何使用 Python 2.7.10 遍历列表并提取引号之间的文本
【发布时间】:2018-02-14 13:03:23
【问题描述】:

我正在尝试遍历一个长列表(我们称之为url_list),其中每个项目看起来像:

<a href="https://www.example.com/5th-february-2018/" itemprop="url">5th February 2018</a>, <a href="https://www.example.com/4th-february-2018/" itemprop="url">4th February 2018</a>, <a href="https://www.example.com/3rd-february-2018/" itemprop="url">3rd February 2018</a>, <a href="https://www.example.com/2nd-february-2018/" itemprop="url">2nd February 2018</a>,

等等。我想遍历列表并只保留前两个引号之间的文本,并丢弃其余的 - 即:

https://www.example.com/5th-february-2018/, https://www.example.com/4th-february-2018/, https://www.example.com/3rd-february-2018/, https://www.example.com/2nd-february-2018/,

所以基本上我试图返回一个很好的干净的 url 列表。我没有太多运气遍历列表并拆分引号 - 有没有更好的方法来做到这一点?有没有办法把itemprop=字符串后面的所有东西都扔掉?

【问题讨论】:

    标签: python regex list text-parsing


    【解决方案1】:

    使用正则表达式:

    import re
    
    url_list = ['<a href="https://www.example.com/5th-february-2018/" itemprop="url">5th February 2018</a>', '<a href="https://www.example.com/4th-february-2018/" itemprop="url">4th February 2018</a>']
    for i in url_list:
        print re.search("(?P<url>https?://[^\s]+)/", i).group("url")
    

    输出:

    https://www.example.com/5th-february-2018
    https://www.example.com/4th-february-2018
    

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 split 函数在 " 处进行拆分,然后从结果列表中取出第二个条目?

      urls=[]
      for url_entry in url_list:
          url = url_entry.split('\"')[1]
          urls.append(url)
      

      【讨论】:

        【解决方案3】:

        这听起来有点像XY problem

        如果您曾经(或正在)使用BeautifulSoup 来解析您的 HTML,它会变得容易得多:

        from bs4 import BeautifulSoup
        
        html_text = '''<a href="https://www.example.com/5th-february-2018/" itemprop="url">5th February 2018</a>
        <a href="https://www.example.com/4th-february-2018/" itemprop="url">4th February 2018</a>
        <a href="https://www.example.com/3rd-february-2018/" itemprop="url">3rd February 2018</a>
        <a href="https://www.example.com/2nd-february-2018/" itemprop="url">2nd February 2018</a>'''
        
        soup = BeautifulSoup(html_text)
        urls = [x['href'] for x in soup.find_all("a")]
        for url in urls:
            print(url)
        # https://www.example.com/5th-february-2018/
        # https://www.example.com/4th-february-2018/
        # https://www.example.com/3rd-february-2018/
        # https://www.example.com/2nd-february-2018/
        

        【讨论】:

          猜你喜欢
          • 2020-08-09
          • 2021-02-08
          • 1970-01-01
          • 1970-01-01
          • 2015-10-27
          • 2021-06-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多