2017-08-09 47 views
-1

我是非常新的蟒蛇和beautifulSoup太..我傾斜網站刮從瑞安mtichell書。 網站我正在刮是http://www.pythonscraping.com/pages/page3.html在python3.6美麗的查詢

from urllib.request import urlopen 
from bs4 import BeautifulSoup 
import re 
html = urlopen("http://www.pythonscraping.com/pages/page3.html") 
bs0bj = BeautifulSoup(html, "html.parser") 
for i in bs0bj.find_all(id="gift1"): 
    print(i.get_text()) 

#for i in bs0bj.find_all("tr", {"class":"gift"}): 
# print(i) 
# for c in bs0bj.find_all("img", {"src":re.compile(\.\.\/img\/gifts/img.*\.jpg)}): 
    #  print(c.image["src"]) 

我的問題是我想用形象的名字一樣... IMG /禮品沿廢只有1排禮品項目頭(「項目,descripion,成本,圖像)。 JPG但直到我不能做索姆有人可以幫我寫正確的代碼

,也請解釋代碼,這樣我可以把它理解太...沒有標籤

+0

可能出現[如何使用BeautifulSoup從特定表中獲取所有行?](https://stackoverflow.com/questions/2010481/how-do-you-get-all-the-從特殊的桌子使用beautifulsoup) – user1211

回答

1

這是你在找什麼?

for i in bs0bj.find_all(id="gift1"): 
    print(i.get_text()) 
    print(i.img.get('src')) 
+0

print(i.img.get('src')),如果你能解釋得到這裏嗎?請和如果我想要每一個圖像的每一行? –

+1

@Prince您在for循環中指定要搜索id爲「gift1」的tr元素中的元素,我們調用get img元素並請求獲取src屬性中包含的文本。 同樣,如果你想打印每一個圖像的每一個圖像的for循環變成: '對於我在bs0bj.find_all(類_ =「禮物」):' – nyvokub

+0

非常感謝你的幫助,先生,這是這裏真的很棒! –

0

下面是代碼

from urllib.request import urlopen 
from bs4 import BeautifulSoup 
import re 
html = urlopen("http://www.pythonscraping.com/pages/page3.html") 
soup = BeautifulSoup(html, "html.parser") 
my_table =soup.find_all("table",id="giftList") 
my_table =my_table[0] 
rows = my_table.findChildren(['th', 'tr']) 
for row in rows: 
    cells = row.findChildren('td') 
    for cell in cells: 
     value = cell.string 
     print ("The value in this cell is %s" % value) 

網上有很多幫助,你可以檢查。

+0

非常感謝你的幫助先生。我真的從你的代碼中學到很多 –

+0

@PrinceBhatia,歡迎。不要忘記接受答案並投票 – user1211