我對Python/BeautifulSoup有點新,並想知道我是否可以在如何完成以下方面獲得一些指導。使用Python + BeautifulSoup一前一後地提取標籤,創建列表列表
我有HTML代碼的網頁,其結構如下:的
1)含有包含所有圖像名稱(名稱1,NAME2 NAME3一個標籤內的代碼塊
2)嵌段包含在具有圖像網址的標籤內的代碼。
3)一個在網頁上出現的日期。我把它放到'date'變量中(這已經被提取出來)
從代碼中,我試圖提取包含[['image1','url1','date']的列表列表,['image2','url2','date']],我將稍後轉換成字典(通過dict(zip(labels,values))函數),並插入到mysql表中。
我能想出的是如何提取兩個包含所有圖像和所有網址的列表。關於如何獲得我想要完成的任何想法?
幾件事情要記住:
1)圖像的數量總是變化,與名字(1沿着:1)
2)日期總是出現一次。
P.S.另外,如果有更優雅的方式通過bs4提取數據,請告訴我!
from bs4 import BeautifulSoup
name = []
url = []
date = '2017-10-12'
text = '<div class="tabs"> <ul><li> NAME1</li><li> NAME2</li><li> NAME3</li> </ul> <div><div><div class="img-wrapper"><img alt="" src="www.image1.com/1.jpg" title="image1.jpg"></img> </div> <center><a class="button print" href="javascript: w=window.open("www.image1.com/1.jpg); w.print();"> Print</a> </center></div><div> <div class="img-wrapper"><img alt="" src="www.image2.com/2.jpg" title="image2.jpg"></img> </div> <center><a class="button print" href="javascript: w=window.open("www.image2.com/2.jpg"); w.print();">Print</a> </center></div><div> <div class="img-wrapper"><img alt="" src="www.image1.com/3.jpg" title="image3.jpg"></img></div> <center><a class="button print" href="javascript: w=window.open("www.image1.com/3.jpg"); w.print();"> Print</a> </center></div> </div></div>'
soup = BeautifulSoup(text, 'lxml')
#print soup.prettify()
#get names
for imgz in soup.find_all('div', attrs={'class':'img-wrapper'}):
for imglinks in imgz.find_all('img', src = True):
#print imgz
url.append((imglinks['src']).encode("utf-8"))
#3 get ad URLS
for ultag in soup.find_all('ul'):
for litag in ultag.find_all('li'):
name.append((litag.text).encode("utf-8")) #dump all urls into a list
print url
print name
'值= [列表(I)+拉鍊[日期]爲我(名稱,網址)]'這樣的事情? –
omg是的。你爲什麼不加區分地把這個置於評論中,而不是作爲答案?謝謝! – FlyingZebra1
目前忙碌..但你可以回答並接受,可能對其他讀者有用。 –