百度图片有些有编码问题,暂时不能爬取,多试几个

#思路:抓取图片地址,根据地址转存图片(注意名称);难点:转码

# -*- coding:utf-8 -*-
from urllib import request,error
import json,re # for page in range(4):
# url = "http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%93%88%E5%A3%AB%E5%A5%87&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=%E5%93%88%E5%A3%AB%E5%A5%87&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn="+str(page*30)+"&rn=30&gsm=1e&1520997016315="
# try:
# response = request.urlopen(url).read().decode("utf-8")
# print(type(response))
#
# except error.URLError as e:
# print(e.reason)
#
class BaiduImg(object):
def __init__(self):
super(BaiduImg,self).__init__()
print('开始采集图片')
self.page = 30
def request(self):
while self.page <= 30:
request_url='http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%93%88%E5%A3%AB%E5%A5%87&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=%E5%93%88%E5%A3%AB%E5%A5%87&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn=30&rn=30&gsm=1e&1520997014923='
# print(len(request_url))
headers = {
'user-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36'
# 'Content-type':'text/html'
# 'Content-type': 'text/html'
}
req=request.Request(request_url,headers=headers)
with request.urlopen(req) as f:
if f.status == 200:
content = f.read().decode('utf-8')
content_dict = json.loads(content)
self.download2(content_dict['data'])
self.page += 30
#下载图片的方法
def dowload(self,data):
for image in data:
if image.get('middleURL'):
url = image['middleURL']
elif image.get('thumbURL'):
url = image['thumbURl']
elif image.get('hoverURL'):
url = image['hoverURL']
else:
url=''
if url:
data = request.urlopen(url).read()
imageName = strip(image['fromPageTitleEnc'])
FileName = str('images/')+imageName+str('.jpg')
with open(FileName,'wb') as f:
f.write(data)
#使用urllib.request.urlretrive()保存图片
# 使用urllib.request.urlretrieve()保存图片
def download2(self, data):
for image in data:
if image.get('middleURL'):
url = image['middleURL']
elif image.get('thumbURL'):
url = image['thumbURL']
else:
url = ""
if url:
imageName = strip(image['fromPageTitleEnc'])
filePath = str('images/') + imageName + str('.jpg')
request.urlretrieve(url, filePath)
#过滤函数
def strip(path):
path = re.sub(r'[?\\*|"<>:/!?]','',str(path))
return path
if __name__ == '__main__':
bi = BaiduImg()
bi.request()

Python_爬虫_百度图片的更多相关文章

  1. python爬虫获取百度图片(没有精华,只为娱乐)

    python3.7,爬虫技术,获取百度图片资源,msg为查询内容,cnt为查询的页数,大家快点来爬起来.注:现在只能爬取到百度的小图片,以后有大图片的方法,我会陆续发贴. #!/usr/bin/env ...

  2. 如何用Python爬虫实现百度图片自动下载?

    Github:https://github.com/nnngu/LearningNotes 制作爬虫的步骤 制作一个爬虫一般分以下几个步骤: 分析需求 分析网页源代码,配合开发者工具 编写正则表达式或 ...

  3. Python爬虫_百度贴吧(title、url、image_url)

    本爬虫以百度贴吧为例,爬取某个贴吧的[所有发言]以及对应发言详情中的[图片链接] 涉及: request 发送请求获取响应 html 取消注释 通过xpath提取数据 数据保存 思路: 由于各贴吧发言 ...

  4. Python_爬虫_案例汇总:

    1.豆瓣采集 1 #coding:utf-8 2 #采集豆瓣书信息和图片,写进数据库 3 4 from urllib import request 5 # from bs4 import Beauti ...

  5. Python爬虫_百度贴吧

    # 本爬虫为爬取百度贴吧并存储HTMLimport requests class TiebaSpider: def __init__(self, tieba_name): self.tieba_nam ...

  6. Python_爬虫_基础

    1.urllib 和  Xpath的区别与联系 from urllib import request from lxml import etree from bs4 import BeautifulS ...

  7. Python_爬虫笔记_2018.3.19

    Python_爬虫_笔记 1.前言 1.1爬虫用途: 网站采集.接口采集(地图(jis 热力学 屋里端口/协议).微信.知乎.) 1.2基本流程:网页下载(requests)+网页解析+爬虫调度 网页 ...

  8. 百度图片爬虫-python版-如何爬取百度图片?

    上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...

  9. 【Python网络爬虫四】通过关键字爬取多张百度图片的图片

    最近看了女神的新剧<逃避虽然可耻但有用>,同样男主也是一名程序员,所以很有共鸣 被大只萝莉萌的一脸一脸的,我们来爬一爬女神的皂片. 百度搜索结果:新恒结衣 本文主要分为4个部分: 1.下载 ...

随机推荐

  1. lumen-ioc容器测试 (1)

    lumen-ioc容器测试 (1) lumen-ioc容器测试 (2) lumen-ioc容器测试 (3) lumen-ioc容器测试 (4) lumen-ioc容器测试 (5) lumen-ioc容 ...

  2. Centos定时备份 MySQL数据库

    一.编写数据库备份脚本 backupmysql.sh #!/bin/bash # Name:bakmysql.sh # This is a ShellScript For Auto DB Backup ...

  3. Vue 过滤器入门

    Vue 允许自定义过滤器,可被用于一些常见的文本格式化 过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 过滤器应该被添加在JavaScript表达式的尾部,由"管道" ...

  4. 关于设置Vscode缩进,保存代码任然缩进无效解决方式

    在Vscode按F1,运行命令,输入Formatter config 把内容更改为以下代码 { "onSave": true, "javascript": { ...

  5. 在Vue中使用Echart图表库。【全网最简单】

    使用npm安装echart npm install echarts --save 然后在使用的页面上直接import import echarts from "echarts"; ...

  6. MacOs/Liunx主机搭建windows平台双机调试环境

    0x00 前言 本文的主要试用对象是Mac OS/Linux用户,对于想调试windows内核相关的一些东西时,需要搭建双机调试环境的一些记录.另外对于本机是windows的用户也完全试用,windo ...

  7. Hadoop框架:HDFS高可用环境配置

    本文源码:GitHub·点这里 || GitEE·点这里 一.HDFS高可用 1.基础描述 在单点或者少数节点故障的情况下,集群还可以正常的提供服务,HDFS高可用机制可以通过配置Active/Sta ...

  8. 转 js调用提交表单。

    今天做网银支付的时候,需要做到点击支付的时候提交订单,然后新窗口打开支付界面. 思路1:window.open(''),这个直接被pass了,因为银行的服务一般都是需要post数据的.就算是可以用ge ...

  9. 使用 Filebeat 对多行日志进行处理(multiline)

    Filebeat 收集日志的过程中,默认是按行收取的,也就是每一行都会默认是一个单独的事件并添加时间戳.但是在收集一些特殊日志的时候,往往一个事件包含有多行,例如 Java 的堆栈跟踪日志: 20-0 ...

  10. 第 1 篇:Vue.js 很高兴认识你

    作者:HelloGitHub--追梦人物 Hello Vue 既然是学习编程,那就遵循一下那个古老的传统仪式. 首先我们新建一个 todos.html 文件,用任何一个你喜欢的文本编辑器或者 IDE ...