跟linux有什么关系,python是跨平台的,爬取图片的代码如下:
import urllib.requestimport osimport randomdef url_open(url):
req=urllib.request.Request(url) #为请求设置user-agent,使得程序看起来更像一个人类
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1WOW64rv:43.0) Gecko/20100101 Firefox/43.0') #代理IP,使用户能以不同IP访问,从而防止被服务器发现
'''iplist=['1.193.162.123:8000','1.193.162.91:8000','1.193.163.32:8000']
proxy_support=urllib.request.ProxyHandler({'http':random.choice(iplist)})
opener=urllib.request.build_opener(proxy_support)
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.154 Safari/537.36 LBBROWSER')]
urllib.request.install_opener(opener)'''
response=urllib.request.urlopen(req)
html=response.read() return htmldef get_page(url):
html=url_open(url).decode('utf-8')
a=html.find('current-comment-page')+23
b=html.find(']',a) #print(html[a:b])
return html[a:b]def find_imgs(url):
html=url_open(url).decode('utf-8')
img_addrs=[]
a=html.find('img src=') while a!=-1:
b=html.find('.jpg',a,a+140) if b!=-1: if html[a+9]!='h':
img_addrs.append('http:'+html[a+9:b+4]) else:
img_addrs.append(html[a+9:b+4]) else:
b=a+9
a=html.find('img src=',b) for each in img_addrs:
print(each+'我的打印') return img_addrsdef save_imgs(folder,img_addrs):
for each in img_addrs: #print('one was saved')
filename=each.split('/')[-1] with open(filename,'wb') as f:
img=url_open(each)
f.write(img)def download_mm(folder='ooxx',pages=10):
os.mkdir(folder)
os.chdir(folder)
url=""
page_num=int(get_page(url)) for i in range(pages):
page_num=page_num-1
page_url=url+'page-'+str(page_num)+'#comments'
img_addrs=find_imgs(page_url)
save_imgs(folder,img_addrs)if __name__=='__main__':
download_mm()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
完成
运行结果
def download_poster_image(movie): #定义一个下载图片函数src = movie #取出它的url
r = requests.get(src) #去获取这个url
fname = url.split('/')[-1] #从url里面获取这个文件名
with open(fname, 'wb') as f: #应答的内容写进去并且使用二进制
f.write(s.content)
movie['poster-path'] = fname res = requests.get(url)
img_url = res.xpath('//img[@class="q-img-item"]/@src')
movie = img_url
download_poster_image(movie) #执行函数
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)