python爬取百度图片
import requests
import re
from urllib import parse
import os
from threading import Thread def download(i,j,key,url):
header = {'content-type': 'application/json',
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Connection":"keep-alive",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Language":"zh-CN,zh;q=0.8"
}
response = requests.get(url,headers=header) link = re.findall(r'"objURL":"(.*?)"',response.text,re.S)
if not os.path.exists(key):
os.mkdir("./"+key+"/")
for web in link:
url = decodeurl(web)
print(url) try:
#allow_redirects=False 关闭重定向
pic = requests.get(url,timeout=10,headers=header,allow_redirects=False)
dirfile = "./"+key+"/" +key + '_' + str(j) + '.jpg'
fp = open(dirfile, 'wb')
fp.write(pic.content)
fp.close()
j += 1
except requests.exceptions.ConnectionError:
print(web,"【错误】当前图片无法下载")
continue
except requests.exceptions.ReadTimeout:
print(web, "【错误】超时")
continue
except requests.exceptions.ChunkedEncodingError:
print(web, "【错误】远程主机强迫关闭了一个现有的连接")
continue def decodeurl(url):
str_table = {
'_z2C$q': ':',
'_z&e3B': '.',
'AzdH3F': '/'
}
char_table = {
'w': 'a',
'k': 'b',
'v': 'c',
'': 'd',
'j': 'e',
'u': 'f',
'': 'g',
'i': 'h',
't': 'i',
'': 'j',
'h': 'k',
's': 'l',
'': 'm',
'g': 'n',
'': 'o',
'r': 'p',
'q': 'q',
'': 'r',
'f': 's',
'p': 't',
'': 'u',
'e': 'v',
'o': 'w',
'': '',
'd': '',
'n': '',
'': '',
'c': '',
'm': '',
'': '',
'b': '',
'l': '',
'a': ''
}
char_table = {ord(key): ord(value) for key, value in char_table.items()}
for key,value in str_table.items():
url = url.replace(key,value)
url = url.translate(char_table)
return url def main():
j = 0
key = "树叶标本"
data = parse.quote(str(key)) for i in range(j,2000,30):
if j == 0:
j +=1
url = "http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord+=&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&word="+data+"&z=&ic=&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&step_word="+data+"&pn="+str(i)+"&rn=30&gsm=3c&1527055161957="
download(i,j,key,url)
j += 30 if __name__ == "__main__":
main()
python爬取百度图片的更多相关文章
- 百度图片爬虫-python版-如何爬取百度图片?
上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...
- python 3 爬取百度图片
python 3 爬取百度图片 学习了:https://blog.csdn.net/X_JS612/article/details/78149627
- Python 爬虫实例(1)—— 爬取百度图片
爬取百度图片 在Python 2.7上运行 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: loveNight import jso ...
- python爬虫-爬取百度图片
python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...
- selenium+chrome浏览器驱动-爬取百度图片
百度图片网页中中,当页面滚动到底部,页面会加载新的内容. 我们通过selenium和谷歌浏览器驱动,执行js,是浏览器不断加载页面,通过抓取页面的图片路径来下载图片. from selenium im ...
- 使用python爬取百度贴吧内的图片
1. 首先通过urllib获取网页的源码 # 定义一个getHtml()函数 def getHtml(url): try: page = urllib.urlopen(url) # urllib.ur ...
- Python爬取谷歌街景图片
最近有个需求是要爬取街景图片,国内厂商百度高德和腾讯地图都没有开放接口,查询资料得知谷歌地图开放街景api 谷歌捷径申请key地址:https://developers.google.com/maps ...
- Python——爬取百度百科关键词1000个相关网页
Python简单爬虫——爬取百度百科关键词1000个相关网页——标题和简介 网站爬虫由浅入深:慢慢来 分析: 链接的URL分析: 数据格式: 爬虫基本架构模型: 本爬虫架构: 源代码: # codin ...
- python 爬取百度url
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-08-29 18:38:23 # @Author : EnderZhou (z ...
随机推荐
- 介绍一下 NDK?
1.NDK 是一系列工具的集合 NDK 提供了一系列的工具,帮助开发者快速开发 C(或 C++)的动态库,并能自动将 so 和 java 应用一起打包成 apk.NDK 集成了交叉编译器,并提供了相应 ...
- 阶段3 2.Spring_01.Spring框架简介_03.spring概述
- Spring 中如何自动创建代理(spring中的三种自动代理创建器)
Spring 提供了自动代理机制,可以让容器自动生成代理,从而把开发人员从繁琐的配置中解脱出来 . 具体是使用 BeanPostProcessor 来实现这项功能. 这三种自动代理创建器 为:Bean ...
- pytest -- 测试的参数化
目录 1. @pytest.mark.parametrize标记 1.1. empty_parameter_set_mark选项 1.2. 多个标记组合 1.3. 标记测试模块 2. pytest_g ...
- Centos7 yum 源安装nginx
一.建立nginx源 vim /etc/yum.repos.d/nginx.repo [nginx]name=nginx repobaseurl=http://nginx.org/packages/c ...
- 人工智能AI------有限状态机、分层状态机、行为树
https://www.cnblogs.com/zhanlang96/p/4793511.html 人工智能遵循着:感知->思考->行动决策方法:有限状态机(Finite-State Ma ...
- eureka和zookeeper的区别?
eureka和zookeeper都可以提供服务注册与发现的功能, zookeeper 是CP原则,强一致性(consistency)和分区容错性(Partition). eur ...
- c/c++ 链表实现
//链表的基本用法代码实现/************************************************************************/ /* Created: ...
- centos7成功部署OpenLDAP
目录 一.部署OpenLDAP. 1 1.安装openLDAP. 1 2.设置openldap管理员密码... 1 3.更改openldap配置... 2 4.更改监控认证配置... 2 5.设置DB ...
- Redis配置主从时报错“Could not connect to Redis at 192.168.0.50:6379: Connection refused not connected>”
配置Redis主从时,修改完从节点配置文件,然后报错 [root@Rich七哥-0-50 redis]# /opt/redis/redis-cli -h 192.168.0.50 Could not ...