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 ...
随机推荐
- 全面解读PHP-数据结构
一.常见数据结构 1.Array 数组 最简单且应用最广泛的数据结构之一 特性:使用连续的内存来存储,数组中的所有元素必须是相同的类型或类型的衍生(同质数据结构),元素可以通过下标直接访问. 2.Li ...
- C++多重继承实践
实践代码如下: #include <iostream> using namespace std; class Animal { private: int hash; public: Ani ...
- matplotlib之折线图
1.案例一 # coding=utf-8 from matplotlib import pyplot as plt import random # 设置字体相关 from matplotlib imp ...
- (WPF)360安全卫士界面设计
点击下载 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&qu ...
- MySQL 数据库下载
地址链接: msi:https://dev.mysql.com/downloads/installer/ zip:https://downloads.mysql.com/archives/commun ...
- 如何理解springcloud微服务项目中,eureka,provider,consumer它们之间的关系?
eureka负责注册provider和consumer的服务信息 provider负责与数据库进行交互,实现数据持久化,并给consumer提供服务 consumer与前端交互,通过与Eureka同源 ...
- 描述什么是springboot
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作<Expert One-On-One J2EE Developme ...
- selenium:css_selector定位详解
selenium:css_selector定位详解(css selector和xpath的比较) 来源:https://www.cnblogs.com/haifeima/p/10138154.html ...
- HTML标签-->列表,表格
只有努力奔跑,才能一直停留在原地. 无序列表 <ul><!--默认列表编号为(实心圆)--> <li>第一项</li> <li>第二项< ...
- 【转】iptables命令、规则、参数详解
表 (table)包含4个表:4个表的优先级由高到低:raw-->mangle-->nat-->filterraw---RAW表只使用在PREROUTING链和OUTPUT链上 ...