首页 > 技术文章 > python爬图 准备多线程

xiaohe520 2019-05-06 19:55 原文

#coding:utf-8
#官方3.0版本已经把urllib2,urlparse等五个模块都并入了urllib中
from urllib import request

import re,urllib,random,time

def getHtml(url):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
page1=request.Request(url,headers=headers)
page=request.urlopen(page1)
# 获取网页内容
html = page.read().decode('utf8')
# print(html)
return html

def getHtml_href(html):
#正则表达式
reg = r'href="(.+?\.html)"'
href = re.compile(reg)
#以列表的形式返回能匹配的子串
hrefList = re.findall(href,html)
href_list=[]
for x in hrefList:
#添加域名保存到列表里面
href_list.append('https://www.113yq.com%s' % x)
return href_list

def getImg(html):
# title_re=r'<h1 class="text-overflow">(.+?)</h1>'

# title = re.findall(re.compile(title_re),html)[0]
timename=time.strftime("%Y_%m_%d %H_%M_%S",time.localtime(time.time()))
#<h1 class="text-overflow">女朋友粉嫩的鲍鱼[11P]</h1>
#正则表达式
reg = r'src="(.+?\.jpg)" alt='
imgre = re.compile(reg)
#以列表的形式返回能匹配的子串
imgList = re.findall(imgre,html.decode('utf-8'))
x=0
for imgurl in imgList:
#把爬取到的资源保存到本地
urllib.request.urlretrieve(imgurl,timename+'%s.jpg' % x)
x+=1
return imgList
#输入你想要爬取的网站
for x in range(2,46):
url='https://www.113yq.com//pic/html28/index_%s.html.html' % x
#url='https://www.113yq.com/pic/html28/index_3.html'
html=getHtml(url)
imgurl_list=getHtml_href(html)
for x in imgurl_list:
print(x)
getImg(x)
#html=getHtml("http://pic.yxdown.com/list/0_0_1.html")
# print(getImg(html))

推荐阅读