繁体   English   中英

如何使用正则表达式提取特定的img src url格式?

[英]How can I extract a specific img src url format using regex?

我的字符串:

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部分以及之后的东西。

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

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

>>> 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']

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

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

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

试试这个脚本:

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"']

尝试这个 :

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

并获得组\\ 1

演示

我怎样才能找到<img src>嵌套在<div>使用美丽的汤?</div><div id="text_translate"><p> Python 和 Beautiful Soup 的新手。 我正在尝试收集插入电子商务网站可折叠部分的img的src 。 包含图像的可折叠部分具有accordion__contents __contents 的 class ,但插入可折叠部分的&lt;img&gt;没有特定的class 。 并非每个页面都包含图像; 有些包含多个。</p><p> 我正在尝试从img中提取随机嵌套在&lt;div&gt;中的src 。 在下面的 HTML 示例中,我想要的 output 将是: &lt;[https://example.com/image1.png]&gt;</p><pre> &lt;div class="accordion__title"&gt;Description&lt;/div&gt; &lt;div class="accordion__contents"&gt; &lt;p&gt;Enjoy Daiya's Hon'y Mustard Dressing on your salads&lt;/p&gt; &lt;/div&gt; &lt;div class="accordion__title"&gt;Ingredients&lt;/div&gt; &lt;div class="accordion__contents"&gt; &lt;p&gt;Non-GMO Expeller Pressed Canola Oil, Filtered Water&lt;/p&gt; &lt;p&gt;&lt;strong&gt;CONTAINS: MUSTARD&lt;/strong&gt;&lt;/p&gt; &lt;/div&gt; &lt;div class="accordion__title"&gt;Nutrition&lt;/div&gt; &lt;div class="accordion__contents"&gt; &lt;p&gt; &lt;img alt="" class="alignnone size-medium wp-image-57054" height="300" src="https://example.com/image1.png" width="162"/&gt; &lt;/p&gt; &lt;/div&gt; &lt;div class="accordion__title"&gt;Warnings&lt;/div&gt; &lt;div class="accordion__contents"&gt; &lt;p&gt;&lt;strong&gt;Contains mustard&lt;/strong&gt;&lt;/p&gt; &lt;/div&gt;</pre><p> 我编写了以下代码,成功深入到完整标签,但是一旦我在那里,我无法弄清楚如何提取src 。</p><pre> img_href = container.find_all(class_ ='accordion__contents') # generates the output above, in a list form img_href = [img.find_all('img') for img in img_href] for x in img_href: if len(x)==0: # skip over empty items in the list that don't have images continue else: print(x) # print to make sure the image is there x.find('img')[`src`] # generates error - see below</pre><p> 我得到的错误是ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()? ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()? 我的意图<em>不是</em>将列表视为一个项目,因此是循环。 我已经尝试find_all()与.attrs('src') ) 结合使用,但这也没有用。 我究竟做错了什么?</p><p> 我已经简化了我的示例,但是我正在抓取的页面的 URL 在<a href="https://gtfoitsvegan.com/product/hony-mustard-dressing-by-daiya/?v=7516fd43adaa" rel="nofollow noreferrer">这里</a>。</p></div>

[英]How can I find <img src> nested within <div> using Beautiful Soup?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用正则表达式提取img标签中的src? 如何在 html img 标签中创建动态 src url? Selenium - 如何检查 xpath 中的元素是否具有特定的 img src url? 有没有办法可以检查 img 标签的 src 是否包含用于使用 BS4 抓取的特定字符串 Scrapy - 使用xPathSelector提取嵌套的'img src' 如何使用 Python 中的正则表达式提取 JSON 字符串的特定部分? 我怎样才能找到<img src>嵌套在<div>使用美丽的汤?</div><div id="text_translate"><p> Python 和 Beautiful Soup 的新手。 我正在尝试收集插入电子商务网站可折叠部分的img的src 。 包含图像的可折叠部分具有accordion__contents __contents 的 class ,但插入可折叠部分的&lt;img&gt;没有特定的class 。 并非每个页面都包含图像; 有些包含多个。</p><p> 我正在尝试从img中提取随机嵌套在&lt;div&gt;中的src 。 在下面的 HTML 示例中,我想要的 output 将是: &lt;[https://example.com/image1.png]&gt;</p><pre> &lt;div class="accordion__title"&gt;Description&lt;/div&gt; &lt;div class="accordion__contents"&gt; &lt;p&gt;Enjoy Daiya's Hon'y Mustard Dressing on your salads&lt;/p&gt; &lt;/div&gt; &lt;div class="accordion__title"&gt;Ingredients&lt;/div&gt; &lt;div class="accordion__contents"&gt; &lt;p&gt;Non-GMO Expeller Pressed Canola Oil, Filtered Water&lt;/p&gt; &lt;p&gt;&lt;strong&gt;CONTAINS: MUSTARD&lt;/strong&gt;&lt;/p&gt; &lt;/div&gt; &lt;div class="accordion__title"&gt;Nutrition&lt;/div&gt; &lt;div class="accordion__contents"&gt; &lt;p&gt; &lt;img alt="" class="alignnone size-medium wp-image-57054" height="300" src="https://example.com/image1.png" width="162"/&gt; &lt;/p&gt; &lt;/div&gt; &lt;div class="accordion__title"&gt;Warnings&lt;/div&gt; &lt;div class="accordion__contents"&gt; &lt;p&gt;&lt;strong&gt;Contains mustard&lt;/strong&gt;&lt;/p&gt; &lt;/div&gt;</pre><p> 我编写了以下代码,成功深入到完整标签,但是一旦我在那里,我无法弄清楚如何提取src 。</p><pre> img_href = container.find_all(class_ ='accordion__contents') # generates the output above, in a list form img_href = [img.find_all('img') for img in img_href] for x in img_href: if len(x)==0: # skip over empty items in the list that don't have images continue else: print(x) # print to make sure the image is there x.find('img')[`src`] # generates error - see below</pre><p> 我得到的错误是ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()? ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()? 我的意图<em>不是</em>将列表视为一个项目,因此是循环。 我已经尝试find_all()与.attrs('src') ) 结合使用,但这也没有用。 我究竟做错了什么?</p><p> 我已经简化了我的示例,但是我正在抓取的页面的 URL 在<a href="https://gtfoitsvegan.com/product/hony-mustard-dressing-by-daiya/?v=7516fd43adaa" rel="nofollow noreferrer">这里</a>。</p></div> 如何在 python 中使用 select 抓取 img src? 如何在页面上找到第一个 img 的 src? 如何将 img src url 获取到 python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM