2016-04-22 89 views
-1
#page source for bs4 
html = wd.page_source 
soup = BeautifulSoup(html, "html.parser") 



thumbnail = (soup.find('div', attrs={ "class" : "preview"})) 

輸出我如何只提取SRC與美麗的湯

[<div class="preview"> 
<img alt="eye.jpg" src="https://thumb-service.domain.net/?sign=d85565637ccacf35673008b12871db54&amp;cdb=CPM&amp;fid=86274&amp;size=120&amp;format=jpg&amp;mtd=maxs&amp;mtdp=&amp;fp=&amp;ts=1461315108317" title="eye.jpg"> 
</img></div>] 

我想只是&爲&的SRC,我已經搜索並嘗試了各種建議,但我不能讓這個

+0

你的意思是你想要的'src'屬性的值,轉義?導航到元素並提取屬性。您是否閱讀了手冊的[*導航樹*部分](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigating-the-tree)?如果你這樣做,你卡在哪裏? –

+0

你的輸出表明你使用'soup.find_all()',而不是'soup.find()'。你有一個* list *,其中有一個元素,而不僅僅是一個元素。 –

回答

2

你可以用CSS selector選擇圖片標籤本身:

thumbnails = soup.select('div.preview img[src]') 
for thumbnail in thumbnails: 
    url = thumbnail['src'] 

上面的CSS選擇器找到<img>標籤,其中src屬性在<div>的類別preview中。

你,你只需要第一匹配,然後使用select_one()

url = soup.select_one('div.preview img[src]')['src']