码迷,mamicode.com
首页 > 编程语言 > 详细

python抓取妹纸图

时间:2016-03-22 12:23:59      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:

import urllib.request
import re
import os

re_img = re.compile(r<p><img src="(.+?)" ></p>)  # 预编译正则, 提高代码效率
re_url = re.compile(rhttp://aimm\.92game\.net/xinggan/(\d+)\.html)

f2 = open("456.txt")
headers = {
    user-agent: Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36,  # 伪造手机UA来访问手机端网页
}
pic_num = 0
os.chdir("G:\\meizhi-image")
for each_url in f2:
    each_url = http://m.aimm.92game.net/n.php?id= + re_url.findall(each_url)[0]  # 把电脑版url转换手机版方便抓取
    url_image = urllib.request.Request(each_url, headers=headers)
    url_image2 = urllib.request.urlopen(url_image).read().decode("utf-8")
    url_image3 = re_img.findall(url_image2)
    for each in url_image3:
        pic_num += 1
        url_image4 = (each)
        path = (str(pic_num) + .jpg)
        print("... ... 第"+str(pic_num)+"只妹纸正在被保存... ...")
        image = urllib.request.urlopen(url_image4)
        image1 = image.read()
        f = open(path, wb)
        f.write(image1)
        f.close()

python3.4 第一只爬虫,主要用到urllib,request。正则表达式。代码比较渣渣

python抓取妹纸图

标签:

原文地址:http://www.cnblogs.com/yefengpython/p/5305657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!