繁体   English   中英

美丽的汤找到嵌套的div

[英]Beautiful Soup find nested div

我正在尝试使用Python-> Beautiful Soup解析一个看起来像这样的网页

看图片

我需要来自的数据

 <div class="p-offer__price-new">199,99 ₽</div> 

我尝试了这段代码:

soup = BeautifulSoup(data)
res = soup.findAll("div", {"class": "poffer__price-new"})
print(res)

但是结果是空的-[]

我如何获得这些数据? URL的示例: https : //edadeal.ru/moskva/offers/d71b75ff-bfee-4731-95ad-52a24ddea72e?from=%2F

import bs4
from selenium import webdriver 

driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')
driver.get('https://edadeal.ru/moskva/offers/d71b75ff-bfee-4731-95ad-52a24ddea72e?from=%2F')

html = driver.page_source

soup = bs4.BeautifulSoup(html,'html.parser')
res = soup.findAll("div", {"class": "p-offer__price-new"})   
print (res[0].text)  

driver.close()

我怎样才能找到<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.

相关问题 使用 Beautiful Soup 查找嵌套的 div/class 如何在美丽的汤中找到 div 美丽的汤嵌套div(添加额外的功能) 使用 Beautiful Soup 在嵌套标签中查找价值 Beautiful Soup 为特定 div 找到孩子 美丽的汤发现所有没有找到 div 在美丽的汤中找到下一个 div 标签 我怎样才能找到<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> 美丽的汤-嵌套表 美丽的汤嵌套表
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM