import requests from bs4 import BeautifulSoup import urllib.response import re
#浏览器伪装 headers={ "user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" } html=requests.get('https://channel.jd.com/home.html').text bs = BeautifulSoup(html, 'html.parser') #找到div下p-img标签 下的a标签的 img标签 avatars = bs.select('div.p-img a img') # 列表推导 找到img标签下的src属性 a=[avatar.attrs.get('src','None') for avatar in avatars]
#存到所需要的路径下 %s为占位符 命名为x每命名一个文件 x+1 x=0 for imgurl in a: urllib.request.urlretrieve('http:' + imgurl, 'D:/4-24\img/%s.jpg' %x) x += 1
print(a.__len__())