源码:
1 import urllib.request 2 from bs4 import BeautifulSoup 3 import os 4 import io 5 #获取网页 6 def get_html(url): 7 headers={‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/60.0.3112.101 Safari/537.36‘} 8 req = urllib.request.Request(url=url,headers=headers) 9 res = urllib.request.urlopen(req) 10 html=res.read() 11 return html 12 #获取图片 13 def get_img(html): 14 links=[] 15 soup=BeautifulSoup(html,‘html.parser‘) 16 result=soup.find_all(‘img‘) 17 for i in result: 18 links.append(‘http:‘+i.get(‘src‘)) 19 return links 20 if __name__==‘__main__‘: 21 url=‘http://jandan.net/ooxx/page-‘ 22 firstpage=1 23 endpage=20 24 j=0 25 for i in range(firstpage,endpage): 26 url1=url+str(i) 27 html=get_html(url1) 28 links=[] 29 links=get_img(html) 30 for link in links: 31 j+=1 32 filename =‘F:\\My\\‘+‘photo‘+str(j)+‘.jpg‘ 33 with open(filename,‘w‘) as file: 34 urllib.request.urlretrieve(link,filename)
时间: 2024-10-16 05:38:02