网页爬虫抓取百度图片( 关于爬虫爬图如何获取网页源代码打开(图) )

优采云 发布时间: 2021-11-25 23:24

  网页爬虫抓取百度图片(

关于爬虫爬图如何获取网页源代码打开(图)

)

  如何使用爬虫爬取图片,以百度图片为例

  关于抓取图片

  最近自己看着网上教程学习如何爬图,发现爬虫的优越性,也发现有些博客对初学者不太友好,因此写了这篇博客。

  如何获取网页的源代码

  打开谷歌浏览器,打开百度图片,点击更多工具->打开开发者工具,如图

  

  可以发现,在underdraw的时候,旁边的Name栏中会有很多图片链接。

  

  随便点一个图片,发现旁边有一个关于Response Headers的内容

  这是你目前访问百度图片的访问信息,这个信息其实是一样的。

  然后在网页上左键点击查看网页的源代码。

  

  再次按下Ctrl/Command+F,使用搜索工具找到url,也就是图片的下载地址,还有一些相关的信息,比如数据等,可以快速找到,体现在代码中。

  

  输入 frompagetitle 以查找图像的标题。

  源代码

  源码中有详细的注释,便于理解。

  # 爬取百度图片

from urllib.parse import urlencode

import requests

import re

import os

# 图片下载的存储文件

save_dir = '百度图片/'

# 百度加密算法

def baidtu_uncomplie(url):

res = ''

c = ['_z2C$q', '_z&e3B', 'AzdH3F']

d = {'w': 'a', 'k': 'b', 'v': 'c', '1': 'd', 'j': 'e', 'u': 'f', '2': 'g', 'i': 'h', 't': 'i', '3': 'j', 'h': 'k',

's': 'l', '4': 'm', 'g': 'n', '5': 'o', 'r': 'p', 'q': 'q', '6': 'r', 'f': 's', 'p': 't', '7': 'u', 'e': 'v',

'o': 'w', '8': '1', 'd': '2', 'n': '3', '9': '4', 'c': '5', 'm': '6', '0': '7', 'b': '8', 'l': '9', 'a': '0',

'_z2C$q': ':', '_z&e3B': '.', 'AzdH3F': '/'}

if (url == None or 'http' in url): # 判断地址是否有http

return url

else:

j = url

# 解码百度加密算法

for m in c:

j = j.replace(m, d[m])

for char in j:

if re.match('^[a-w\d]+$', char): # 正则表达式

char = d[char]

res = res + char

return res

# 获取页面信息

def get_page(offset):

params = {

'tn': 'resultjson_com',

'ipn': 'rj',

'ct': '201326592',

'is': '',

'fp': 'result',

'queryWord': '中国人', # 关键字

'cl': '2',

'lm': '-1',

'ie': 'utf-8',

'oe': 'utf-8',

'adpicid': '',

'st': '-1',

'z': '',

'ic': '0',

'word': '中国人', # 关键字

's': '',

'se': '',

'tab': '',

'width': '',

'height': '',

'face': '0',

'istype': '2',

'qc': '',

'nc': '1',

'fr': '',

'expermode': '',

'pn': offset * 30,

'rn': '30',

'gsm': '1e',

'1537355234668': '',

}

url = 'https://image.baidu.com/search/acjson?' + urlencode(params)

try: # 尝试连接服务器

response = requests.get(url)

if response.status_code == 200: # 获取HTTP状态,即服务器响应HTTP请求

return response.json()

except requests.ConnectionError as d:

print('Error', d.args)

# 获取图像

def get_images(json):

if json.get('data'):

for item in json.get('data'): # 获取图片数据字典值

if item.get('fromPageTitle'): # 获取图片Title

title = item.get('fromPageTitle')

else:

title = 'noTitle'

image = baidtu_uncomplie(item.get('objURL')) # 图片地址

if (image):

yield { # 存储图片信息

'image': image,

'title': title

}

def save_image(item, count):

try:

response = requests.get(item.get('image'))

if response.status_code == 200: # 获取HTTP状态,即服务器响应HTTP请求

file_path = save_dir + '{0}.{1}'.format(str(count), 'jpg') # 命名并存储图片

if not os.path.exists(file_path): # 判断图片是否在文件中

with open(file_path, 'wb') as f:

f.write(response.content)

else:

print('Already Downloaded', file_path)

except requests.ConnectionError: # 如果出现连接错误

print('Failed to Save Image')

def main(pageIndex, count):

json = get_page(pageIndex)

for image in get_images(json):

save_image(image, count)

count += 1

return count

if __name__ == '__main__':

if not os.path.exists(save_dir): # 判断是否存在文件,若没有则创建一个

os.mkdir(save_dir)

count = 1

for i in range(1, 200): # 循环页数下载图片

count = main(i, count) # i表示页数,统计图片并运行主函数

print('total:', count)

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线