【问题标题】:How to extract some url from html?如何从html中提取一些url?
【发布时间】:2021-12-11 01:36:21
【问题描述】:

我需要从本地 html 文件中提取所有图像链接。不幸的是,我无法安装bs4cssutils 来处理html。

html = """<img src="https://s2.example.com/path/image0.jpg?lastmod=1625296911"><br>
<div><a style="background-image:url(https://s2.example.com/path/image1.jpg?lastmod=1625296911)"</a><a style="background-image:url(https://s2.example.com/path/image2.jpg?lastmod=1625296912)"></a><a style="background-image:url(https://s2.example.com/path/image3.jpg?lastmod=1625296912)"></a></div>"""

我尝试使用正则表达式提取数据:

images = []
for line in html.split('\n'):
    images.append(re.findall(r'(https://s2.*\?lastmod=\d+)', line))
print(images)

[['https://s2.example.com/path/image0.jpg?lastmod=1625296911'],
 ['https://s2.example.com/path/image1.jpg?lastmod=1625296911)"</a><a style="background-image:url(https://s2.example.com/path/image2.jpg?lastmod=1625296912)"></a><a style="background-image:url(https://s2.example.com/path/image3.jpg?lastmod=1625296912']]

我想我的正则表达式是greedy,因为我使用了.*? 如何得到以下结果?

images = ['https://s2.example.com/path/image0.jpg',
          'https://s2.example.com/path/image1.jpg',
          'https://s2.example.com/path/image2.jpg',
          'https://s2.example.com/path/image3.jpg']

如果有帮助,所有链接都用src="..."url(...)括起来

感谢您的帮助。

【问题讨论】:

    标签: python html regex


    【解决方案1】:
    import re
    xx = '<img src="https://s2.example.com/path/image0.jpg?lastmod=1625296911" alt="asdasd"><img a src="https://s2.example.com/path/image0.jpg?lastmod=1625296911">'
    r1 = re.findall(r"<img(?=\s|>)[^>]*>",xx)
    url = []
    for x in r1:
      x = re.findall(r"src\s{0,}=\s{0,}['\"][\w\d:/.=]{0,}",x)
      if(len(x)== 0): continue
      x = re.findall(r"http[s]{0,1}[\w\d:/.=]{0,}",x[0])
      if(len(x)== 0): continue
      url.append(x[0])
    print(url)
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    你可以使用

    import re
    html = """<img src="https://s2.example.com/path/image0.jpg?lastmod=1625296911"><br>
    <div><a style="background-image:url(https://s2.example.com/path/image1.jpg?lastmod=1625296911)"</a><a style="background-image:url(https://s2.example.com/path/image2.jpg?lastmod=1625296912)"></a><a style="background-image:url(https://s2.example.com/path/image3.jpg?lastmod=1625296912)"></a></div>"""
    images = re.findall(r'https://s2[^\s?]*(?=\?lastmod=\d)', html)
    print(images)
    

    请参阅Python demo。输出:

    ['https://s2.example.com/path/image0.jpg',
     'https://s2.example.com/path/image1.jpg',
     'https://s2.example.com/path/image2.jpg', 
     'https://s2.example.com/path/image3.jpg']
    

    也请参阅regex demo。这意味着

    • https://s2 - 一些文字文本
    • [^\s?]* - 除空格和? 字符以外的零个或多个字符
    • (?=\?lastmod=\d) - 紧靠右边,必须有 ?lastmode= 和一个数字(文本不会添加到匹配中,因为它是正向前瞻中的模式,非消耗模式)。

    【讨论】:

      【解决方案3】:
      import re
      indeces_start = sorted(
          [m.start()+5 for m in re.finditer("src=", html)]
          + [m.start()+4 for m in re.finditer("url", html)])
      indeces_end = [m.end() for m in re.finditer(".jpg", html)]
      
      image_list = []
      
      for start,end in zip(indeces_start,indeces_end):
        image_list.append(html[start:end])
      
      print(image_list)
      

      这是我想到的解决方案。它包括查找图像路径字符串的开始和结束索引。如果有不同的图像类型,显然必须进行调整。

      编辑:更改了启动条件,以防文档中有其他 URL

      【讨论】:

        猜你喜欢
        • 2018-01-08
        • 2019-08-19
        • 2011-07-09
        • 2011-08-29
        • 2019-09-07
        • 1970-01-01
        • 1970-01-01
        • 2017-07-27
        • 1970-01-01
        相关资源
        最近更新 更多