【问题标题】:Find with regex and get value from img tag which is member of another tag使用正则表达式查找并从另一个标签的成员 img 标签中获取值
【发布时间】:2020-09-15 19:03:16
【问题描述】:

我有来自网站的代码,我想在其中找到并获取图像的路径,我的意思是来自 src 标记中的 img 的值。问题的核心是标签之间的图像路径:wphimage.jpg 扩展。

代码下方:

<p>
    <wphimage data="{'Copyright':'John Smith','Alignment':'left','ImageVersion':'conductorportraitlong'}">
    <span style="display:block; float:left;" class="DIV_imageWrapper">
        <a data-lightview-title="John Smith"  class="lightview" href="//path/to/image/web.jpg"">
            <img src="//path/to/image/web.jpg" alt="Name">
        </a>
        <a class="A_copyright" href="javascript:;">©&nbsp; <span>Terry Linke</span></a>
        <a href="javascript:;">≡ <span>John Smith</span></a>| 
        <a class="A_zoom lightview" href="//path/to/image/web.jpg" data-lightview-title="Dietfried Gürtler" data-lightview-caption="Terry Linke">+ </a>
    </span>
    </wphimage>

    Text here...
</p>

我试过了:

wphimage = re.findall(r'\S+\.jpg', text)

但我还得到了另一个值,来自不同的标签,然后是 &lt;img&gt;

【问题讨论】:

标签: python html regex tags


【解决方案1】:

你可以试试

wphimage = re.findall(r'<img.*src=\"(\S*|\w*)\"', txt)

输出

['//path/to/image/web.jpg']

此正则表达式正在获取 img 标记 src 值中“”之间的任何非空格和任何单词字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多