Python-爬虫-抓取头条街拍图片-1.1

下面实例是抓取头条图片信息,只是抓取了查询列表返回的json中image,大图标,由于该结果不会包含该链接详情页的所有图片列表;因此这里抓取不全;后续有时间在完善;

1、抓取头条街拍相关图片请求如下:

2、通过debug可以看到请求参数以及相应结果数据:

3、响应结果,比较重要的是data(group_id,image_list、large_image_url等字段):

主程序如下:

抓取图片信息保存本地,然后将图片组和图片信息保存至mysql数据库;

  1 #今日头条街拍数据抓取,将图片存入文件目录,将文件目录存放至mysql数据库
  2 import requests
  3 import  time
  4 from urllib.parse import urlencode
  5 import urllib.parse
  6 import os
  7 from requests import Request, Session
  8 import pymysql
  9 class TouTiaoDeep:
 10     def __init__(self):
 11         self.url=‘https://www.toutiao.com/search_content/‘
 12         self.imagePath=‘D:/toutiao/images/‘
 13         self.headers={
 14             ‘Accept‘:‘application/json, text/javascript‘,
 15             ‘Accept-Encoding‘:‘gzip, deflate, br‘,
 16             ‘Content-Type‘:‘application/x-www-form-urlencoded‘,
 17             ‘Host‘: ‘www.toutiao.com‘,
 18             ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0‘,
 19             ‘X-Requested-With‘: ‘XMLHttpRequest‘
 20         }
 21         self.param={
 22             ‘offset‘:0,
 23             ‘format‘:‘json‘,
 24             ‘keyword‘:  ‘街拍‘,
 25             ‘autoload‘:‘true‘,
 26             ‘count‘:20,
 27             ‘cur_tab‘:1,
 28             ‘form‘:‘search_tab‘,
 29             ‘pd‘:‘synthesis‘
 30          }
 31         self.filePath="D:/toutiaoImages"
 32         self.imgDict={} #{rows:[{title:‘‘,pathName:‘‘,images:[{name:‘‘,desc:‘‘,date:‘‘,downloadUrl:‘‘}...},...] ]}
 33
 34     def getImgDict(self,offset):
 35         self.param[‘offset‘]=offset#偏移量
 36         session=Session()
 37         req=Request(method=‘GET‘,url=self.url ,params=self.param,headers=self.headers  )
 38         prep =  session.prepare_request(req)
 39         res = session.send(prep)
 40         #print(res.status_code)
 41         if res.status_code==200:
 42             json=res.json()
 43             #print(json)
 44             for i in range(len(json[‘data‘])):
 45                 if  ‘has_image‘ in json[‘data‘][i].keys() and json[‘data‘][i][‘has_image‘]:#其中有视频列表组,因此排除那些视频组
 46                     # print("标题:",json[‘data‘][i][‘title‘])
 47                     # print("图库:",json[‘data‘][i][‘image_list‘])
 48                     # print("图库简介:",json[‘data‘][i][‘abstract‘])
 49                     # print("图片个数:",(len(json[‘data‘][i][‘image_list‘])))
 50                     yield {
 51                         ‘group_id‘:json[‘data‘][i][‘group_id‘],
 52                         ‘groupTitle‘:json[‘data‘][i][‘title‘],
 53                         ‘groupImages‘:json[‘data‘][i][‘image_list‘],
 54                         ‘total‘:len(json[‘data‘][i][‘image_list‘]),
 55                         ‘abstract‘:json[‘data‘][i][‘abstract‘],
 56                         ‘large_image_url‘:json[‘data‘][i][‘large_image_url‘][:json[‘data‘][i][‘large_image_url‘].rindex(‘/‘)]
 57                         # 例如:http://p3-tt.bytecdn.cn/large/pgc-image/2dc7e3cd2e0c46f69ee67c11c13ff58e  最后一个是图片id,前面是大图片地址(每一组大图片地址不同)
 58                         # print(item[‘large_image_url‘][:item[‘large_image_url‘].rindex(‘/‘)])#获取组大图片的地址url
 59                     }
 60     def imagesDownLoad(self,offset):
 61         # 获得当前时间时间戳
 62         now = int(time.time())
 63         #转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
 64         timeStruct = time.localtime(now)
 65         strTime = time.strftime("%Y-%m-%d %H:%M:%S", timeStruct)
 66
 67         datas=self.getImgDict(offset)
 68         for item in datas:
 69             #print(item)
 70             #下载图片信息
 71             groupImages=item[‘groupImages‘]
 72             print(item[‘groupTitle‘])
 73             for i in groupImages:
 74                 #print(i[‘url‘][(i[‘url‘].rindex(‘/‘)):])截取图片id即,图片地址最有一个namespace
 75                 imgURL=item[‘large_image_url‘]+i[‘url‘][(i[‘url‘].rindex(‘/‘)):]#拼成完成的image URL
 76                 print(imgURL)
 77                 #创建存储文件夹,组id命名
 78                 if not os.path.exists(self.imagePath+item[‘group_id‘]):
 79                     os.makedirs(self.imagePath+item[‘group_id‘])
 80                 #获取图片存上面指定目录中
 81                 try:
 82                     a = urllib.request.urlopen(imgURL)
 83                 except :
 84                       a=urllib.request.urlopen("http://p1.pstatp.com/origin/pgc-image/"+i[‘url‘][(i[‘url‘].rindex(‘/‘)):])#注意有一部分图片url路径是:http://p1.pstatp.com/origin/pgc-image/7290e8fcfdbc4a458d8ed7a6c1581283[前面的p1 可以任意换成p任意数字即可]
 85                       #注意;改程序在二十左右页抓取会出现图片路径资源错误
 86                 try:
 87                     f = open(self.imagePath+item[‘group_id‘]+"/"+i[‘url‘][(i[‘url‘].rindex(‘/‘)):]+‘.jpg‘, "wb")
 88                     f.write(a.read())
 89                     f.close()
 90                     #持久化图片信息
 91                     rows_1={
 92                         ‘imageId‘: i[‘url‘][(i[‘url‘].rindex(‘/‘)):],
 93                         ‘imagesource‘: imgURL,
 94                         ‘imageName‘:i[‘url‘][(i[‘url‘].rindex(‘/‘)):]+‘.jpg‘,
 95                         ‘imageDesc‘: ‘无‘,
 96                         ‘groupid‘: item[‘group_id‘]
 97                     }
 98                     self.imageInfPersistent(rows_1)
 99                 except:
100                     print(‘文件下载失败‘)
101             #持久化图片组信息
102             rows_2 = {
103                 ‘groupid‘:item[‘group_id‘],
104                 ‘grouptitle‘:item[‘groupTitle‘],
105                 ‘groupdesc‘:item[‘abstract‘],
106                 ‘path‘:‘toutiao/images/‘+item[‘group_id‘],
107                 ‘createTime‘:strTime
108             }
109             self.imgGroupPersistent(rows_2)
110
111
112
113     #mysql数据库持久化
114     def mysqlPersistent(self,tableName,data):
115         db = pymysql.connect(host=‘localhost‘, user=‘root‘, password=‘admin‘, port=3306, db=‘test‘)
116         cursor = db.cursor()
117         try:
118             columns = ‘,‘.join(data.keys())
119             values = ‘,‘.join([‘%s‘] * len(data))
120             sql =  ‘insert into {table}({keys}) VALUES ({values}) ‘.format(table=tableName, keys=columns, values=values)
121             cursor.execute(sql, tuple(data.values()))
122             db.commit()
123         except:
124             db.rollback()
125         finally:
126             db.close()
127
128     #持久化图片组信息
129     def imgGroupPersistent(self,groupDict):
130         #图组信息表:组id、组标题、组简介、本地存储路径、创建时间
131        self.mysqlPersistent(‘imageGroup‘,groupDict)
132
133     #持久化图片信息
134     def imageInfPersistent(self,imageInfDict):
135         #图片信息表:图片id、来源地址、简介、所属组id
136         self.mysqlPersistent(‘imageInfo‘, imageInfDict)
137
138     #创建表
139     def createImgTable(self):
140         sql_imgGroup= ‘create table imageGroup(groupid varchar(50) primary key,grouptitle varchar(200)  ,groupdesc text,path varchar(500),createTime varchar(50))‘
141         sql_imgInf=‘create table imageInfo(imageId varchar(50) primary key,imagesource varchar(200) ,imageName varchar(100),imageDesc text,groupid varchar(50) )‘
142         db = pymysql.connect(host=‘localhost‘, user=‘root‘, password=‘admin‘, port=3306, db=‘test‘)
143
144         cursor = db.cursor()
145         try :
146             cursor.execute(sql_imgGroup)
147             cursor.execute(sql_imgInf)
148         except:
149             print(‘表创建失败!‘)
150         finally:
151             cursor.close()
152
153     #删除表
154     def dropImgTables(self):
155         sql_dropImageGroup = ‘ drop table if exists  imageGroup ‘
156         sql_dropImageInfo = ‘  drop table if exists   imageInfo ‘
157         db = pymysql.connect(host=‘localhost‘, user=‘root‘, password=‘admin‘, port=3306, db=‘test‘)
158
159         cursor = db.cursor()
160         try:
161             cursor.execute(sql_dropImageGroup)
162             cursor.execute(sql_dropImageInfo)
163         except:
164             print(‘表删除失败!‘)
165         finally:
166             cursor.close()
167
168
169 if __name__==‘__main__‘:
170     deep=TouTiaoDeep()
171     deep.dropImgTables()#删除表
172     deep.createImgTable()#创建表
173     #print(deep.getImgDict())
174     for i in range(0,10*20,10):
175         deep.imagesDownLoad(i)
176     #deep.createImgTable()

操作后结果:注意,由于图片url拼接不能完全百分百正确,因此抓取数据会因为图片地址错误报异常;

原文地址:https://www.cnblogs.com/ygzhaof/p/10267090.html

时间: 2024-08-29 08:47:07

Python-爬虫-抓取头条街拍图片-1.1的相关文章

python爬虫抓取色影无忌月赛获奖图片

因为平时爱好摄影,所以喜欢看看色影无忌论坛的获奖摄影作品,所以写了个小script用来抓取上面的获奖图片,亲自测试可以使用. # -*- coding: UTF-8 -*- #作者Rocky Chen import re, urllib, sys, os, time, urllib2, cookielib, string class Download: def __init__(self, url): self.url=url def getPhotos(self): #获取的是跳转收的各个页面

Python爬虫抓取网页图片

本文通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地. 下面就看看如何使用python来实现这样一个功能. # -*- coding: utf-8 -*- import urllib import re import time import os #显示下载进度 def schedule(a,b,c): ''''' a:已经下载的数据块 b:数据块的大小 c:远程文件的大小 ''' per = 100.0 * a * b / c if per > 100 : per =

python 爬虫抓取心得

quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quote('要编码的字符串') query = urllib.quote(singername) url = 'http://music.baidu.com/search?key='+query response = urllib.urlopen(url) text = response.read()

python 爬虫抓取心得分享

/** author: insun title:python 爬虫抓取心得分享 blog:http://yxmhero1989.blog.163.com/blog/static/112157956201311821444664/ **/    0x1.urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quote('要编码的字符串') query = urllib.quote(singername) url = 'h

python爬虫抓取站长之家IP库,仅供练习用!

python爬虫抓取站长之家IP库,单线程的,仅供练习,IP库数据有43亿条,如果按此种方法抓取至少得数年,所以谨以此作为练手,新手代码很糙,请大家见谅. #!/usr/bin/python #coding=UTF-8 import urllib2 import re import os import csv import codecs user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-

python爬虫抓取哈尔滨天气信息

python 爬虫 爬取哈尔滨天气信息 - http://www.weather.com.cn/weather/101050101.shtml 环境: windows7 python3.4(pip install requests:pip install BeautifulSoup4) 代码:(亲测可以正确执行) 1 # coding:utf-8 2 """ 3 总结一下,从网页上抓取内容大致分3步: 4 1.模拟浏览器访问,获取html源代码 5 2.通过正则匹配,获取指定

Python爬虫抓取技术的门道

web是一个开放的平台,这也奠定了web从90年代初诞生直至今日将近30年来蓬勃的发展.然而,正所谓成也萧何败也萧何,开放的特性.搜索引擎以及简单易学的html.css技术使得web成为了互联网领域里最为流行和成熟的信息传播媒介:但如今作为商业化软件,web这个平台上的内容信息的版权却毫无保证,因为相比软件客户端而言,你的网页中的内容可以被很低成本.很低的技术门槛实现出的一些抓取程序获取到,这也就是这一系列文章将要探讨的话题-- 网络爬虫 . 有很多人认为web应当始终遵循开放的精神,呈现在页面

Python爬虫抓取糗百的图片,并存储在本地文件夹

思路: 1.观察网页,找到img标签 2.通过requests和BS库来提取网页中的img标签 3.抓取img标签后,再把里面的src给提取出来,接下来就可以下载图片了 4.通过urllib的urllib.urlretrieve来下载图片并且放进文件夹里面(第一之前的准备工作就是获取当前路径然后新建一个文件夹) 5.如果有多张图片,不断的重复3-4 由于爬虫写得少,通过自己的调试,终于写了出来了 下面直接上代码: 1 #coding = 'utf-8' 2 import requests 3 f

Python爬虫 —— 抓取美女图片

代码如下: 1 #coding:utf-8 2 # import datetime 3 import requests 4 import os 5 import sys 6 from lxml import etree 7 import codecs 8 9 class Spider: 10 def __init__(self): 11 self.headers = {} 12 self.headers['User_Agent'] = 'Mozilla/5.0 (Windows NT 10.0;