【问题标题】:How can I extract a specific img src url format using regex?如何使用正则表达式提取特定的 img src url 格式?
【发布时间】:2016-06-08 12:36:50
【问题描述】:

我的字符串:

Russia's National Settlement Depository discusses why it believes the biggest blockchain opportunities have yet to be uncovered.<img alt="" height="1" src="http://feeds.feedburner.com/~r/CoinDesk/~4/rvoQUj-KDaw" width="1" />|One of the co-founder of digital currency startup Stellar announced their resignation today.<img alt="" height="1" src="http://feeds.feedburner.com/~r/CoinDesk/~4/xRzN7syt-v0" width="1" />|The editorial board for Bloomberg News has called for a permissive regulatory environment for blockchain development.<img alt="" height="1" src="http://feeds.feedburner.com/~r/CoinDesk/~4/ooQYB2iDxP8" width="1" />|

我想把这 3 个链接放到一个列表中:

http://feeds.feedburner.com/~r/CoinDesk/~4/rvoQUj-KDaw
http://feeds.feedburner.com/~r/CoinDesk/~4/xRzN7syt-v0
http://feeds.feedburner.com/~r/CoinDesk/~4/ooQYB2iDxP8

他们遵循这种模式:

src="http://feeds.feedburner.com/~r/CoinDesk/~4/rvoQUj-KDaw"

我知道我应该使用re.findall(pattern, string) 来实现这一点。

但最大的问题是:我怎样才能建立一个在这里工作的模式?

我不太擅长编写正则表达式模式。我总是感到困惑...几乎完成工作的是这个:

pattern = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'

但我得到的只是这个列表:

[u'http://feeds.feedburner.com/', u'http://feeds.feedburner.com/', u'http://feeds.feedburner.com/']

看起来问题出在~r 部分和之后的东西上。

【问题讨论】:

    标签: python regex url extract src


    【解决方案1】:

    这些数据来自哪里?我建议使用 html 解析器而不是尝试使用正则表达式进行提取。如果来自 html,您可以从那里的标签中提取完整值

    下面我把你的字符串放在 test.html 中(对于 python 使用 beautifulsoup 为例)

    >>> from bs4 import BeautifulSoup
    >>> soup = BeautifulSoup(open(r'A:\test.html'))
    >>> [x['src'] for x in soup.findAll('img')]
    ['http://feeds.feedburner.com/~r/CoinDesk/~4/rvoQUj-KDaw', 'http://feeds.feedburner.com/~r/CoinDesk/~4/xRzN7syt-v0', 'http://feeds.feedburner.com/~r/CoinDesk/~4/ooQYB2iDxP8']
    

    【讨论】:

    • 虽然这是一个很好的建议,但它并不试图回答这个问题。
    • 抱歉...已修复:)
    • 正则表达式可能出错的地方太多,这将是我的首选。
    【解决方案2】:

    您的正则表达式中缺少~ 字符:

    http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&amp;+~]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+

    顺便说一句:this 是在 Python 中测试正则表达式的超级方法!

    【讨论】:

    • 我一直在使用 regex101,但我喜欢你的网站!书签!谢谢
    【解决方案3】:

    试试这个脚本:

    text1="""Russia's National Settlement Depository discusses why it believes the biggest blockchain opportunities have yet to be uncovered.<img alt="" height="1" src="http://feeds.feedburner.com/~r/CoinDesk/~4/rvoQUj-KDaw" width="1" />|One of the co-founder of digital currency startup Stellar announced their resignation today.<img alt="" height="1" src="http://feeds.feedburner.com/~r/CoinDesk/~4/xRzN7syt-v0" width="1" />|The editorial board for Bloomberg News has called for a permissive regulatory environment for blockchain development.<img alt="" height="1" src="http://feeds.feedburner.com/~r/CoinDesk/~4/ooQYB2iDxP8" width="1" />|"""
    import re
    print re.findall(r'(https?://\S+)', text1)
    

    结果是

    ['http://feeds.feedburner.com/~r/CoinDesk/~4/rvoQUj-KDaw"',   'http://feeds.feedburner.com/~r/CoinDesk/~4/xRzN7syt-v0"', 'http://feeds.feedburner.com/~r/CoinDesk/~4/ooQYB2iDxP8"']
    

    【讨论】:

    • 哇!这是获取所有这些信息的简单方法。看来我的头脑过于复杂了!
    【解决方案4】:

    试试这个:

    (?:src=)(".*?")
    

    并获得组 \1

    DEMO

    【讨论】:

    • 否决票没有问题..但解释为什么..这样我会纠正我自己..ty.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多