|
楼主 |
发表于 2020-6-5 16:24:14
|
显示全部楼层
还是不得行,如果我只是下载图1的照片的话,不改也是能下载到的,但进图片集合就会显示出错的图片
import re
import urllib.request
import requests
import os
import random
'''
dai=['222.90.110.194:8080', '223.241.20.245:30001', '27.46.23.18:888']
ip=random.choice(dai)
proxy_support=urllib.request.ProxyHandler({'https':ip})
opener=urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
'''
#取得html
def open_url(url):
header={'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0'}
res=requests.get(url,headers=header)
res.encoding='gbk'
html=res.text
return html
#图片html
def open_urlt(url):
header={'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0'}
res=requests.get(url,headers=header)
res.encoding='gbk'
html=res.content
return html
#取得图片并保存
def get_img(html):
p=r'<img id="bigImg" src="([^"]+\.jpg)"'
imglist=re.findall(p,html)
for each in imglist:
filename=each.split('/')[-1]
urllib.request.urlretrieve(each,filename,None) #取代了 with open(filename,'wb') as f:
# img=open_url(each)
# f.write(img)
#取得网页的页面
def get_page(html):
p=r'<a href="([^"l]+\.html)"'
pagelist=re.findall(p,html)
pages_url=[]
for i in range(len(pagelist)):
pagelist_new='http:/www.jj20.com'+pagelist[i]
pages_url.append(pagelist_new)
return pages_url
#主程序
def main_url(folder='jj20zha'):
if os.path.exists(folder)==False:
os.mkdir(folder)
os.chdir(folder)
url='http://www.jj20.com/bz/nxxz/'
#获得网页同一页面下不同的图片集合
html=open_url(url)
pages=get_page(html)
for each in pages:
each_new=each+'#q'
print(each_new)
html_page=open_urlt(each) ############问题在这里,前面测试打印都没问题,但不不明白为什么问题在这,这里是把图片集合的地址变成html再传给下面的get_img(htmL)
#获得并保存图片
get_img(html_page)
if __name__=='__main__':
main_url()
|
|