簡體   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