import requests
from requests.exceptions import RequestException
import re
import time
number=0
def get_one_html(url):
try:
response=requests.get(url)
response.encoding = 'utf-8' # 定义编码
if response.status_code==200:
return response.text
return None
except RequestException:
return None
def get_all_url():
for i in range(1,20):
url="http://www.tu11.com/xingganmeinvxiezhen/list_1_{}.html".format(i)
time.sleep(1)
html=get_one_html(url)
#print(html)
reuserid = re.compile(' <div class="shupic".*?a href="(.*?)" target="_blank"><img class="', re.S)
itemsuser = re.findall(reuserid, html)
get_jpg_url(itemsuser)
def get_jpg_url(items):
for item in items:
html = get_one_html("http://www.tu11.com"+item)
get_number_jpg(html)
for i in range(1,get_number_jpg(html)):
if i>1:
a = item.strip()[:-5]+"_{}.html".format(i)
print(a)
else:
a=item
print(a)
html = get_one_html("http://www.tu11.com" + a)
reuserid = re.compile('<p align="cen.*?img src="(.*?)".*?</p><br/', re.S)
items = re.findall(reuserid,html)
if len(items)>0:
print(items)
write_ipg_loding(items)
def get_number_jpg(html): #获得当前页数
reuserid = re.compile('ist-inline text-center nryfy"><li><a>��(.*?)ҳ:', re.S)
item = re.findall(reuserid, html)
return int(item[0])
def write_ipg_loding(images):
global number
for image in images:
response=requests.get(image)
with open('C:\\Users\\Administrator\\Desktop\\test\\'+str(number) + '.jpg', 'wb') as f:
print("正在写入第%d"%number)
f.write(response.content)
f.close()
number=number+1
if __name__=="__main__":
get_all_url()
#/xingganmeinvxiezhen/2018/10900_2.html
讯享网
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/120198.html