2017-08-09 52 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