吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5197|回复: 14
收起左侧

[Python 转载] 百度图片下载爬虫

[复制链接]
ping1ggg 发表于 2017-3-12 13:26
本帖最后由 ping1ggg 于 2017-3-12 13:27 编辑

#-*- coding:uft-8
import re
import urllib
import urllib2
def get_content(url):
    """doc."""
    req = urllib2.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:46.0) Gecko/20100101 Firefox/46.0')
    req.add_header('GET',url)
    req.add_header('Host','tieba.baidu.com')#主机
    req.add_header('Referer','http://tieba.baidu.com/f?kw=%E6%A1%8C%E9%9D%A2&tab=album&red_tag=a0654939890')
    html = urllib2.urlopen(req)
    content = html.read()
    html.close()

    return content


def get_images(info):

    regex = r'https://imgsa.baidu.com/forum/abpic/item/.+.jpg'

    pat = re.compile(regex)
    images_code = re.findall(pat,info)

    i = 0
    for image_url in images_code:
        print image_url

        #urllib.urlretrieve(image_url,'%s.jpg' %i)
        urllib.urlretrieve(image_url,'%s.jpg' %image_url[41:-4])
        i+=1
        



url = "https://tieba.baidu.com/p/1457328460#!/l/p1"
content = get_content(url)
#info = content.replace('\\','')
#print info
get_images(content)

免费评分

参与人数 2吾爱币 +2 热心值 +1 收起 理由
华说要有光 + 1 + 1 热心回复!
在线客服 + 1 那么问题来了,怎么应用到自己的网站?

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| ping1ggg 发表于 2017-3-13 22:08
no_009 发表于 2017-3-13 21:02
学过一点,不过没怎么用urllib2.Request类。.add_header()方法挺方便的。
学习了

嗯  是的
 楼主| ping1ggg 发表于 2017-3-12 20:09

这个程序本身没用处,但是提供了一种思路,简单的给出了怎么抓取数据的方法
tujinfeng 发表于 2017-3-12 14:01
ybsypy 发表于 2017-3-12 14:39
好东西,谢谢分享!
爱挠头 发表于 2017-3-12 14:42
好东西,谢谢分享
阿墨 发表于 2017-3-12 15:15
这个怎么用?
qq491354460 发表于 2017-3-12 17:18
感谢楼主,复制来研究一下
youlong34 发表于 2017-3-13 15:44
Python效率不高 不过开发快捷 还是值得学习一下
aristotllgood 发表于 2017-3-13 22:22
爬虫还是早点升级到 Python3 比较好
到底 Unicode 相关的问题太蛋疼

urllib urllib2 在三里面合并成一个
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-26 08:12

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表