python实现爬取百度图片的方法示例

作者:双歧杆菌 时间:2021-11-22 00:46:04 

本文实例讲述了python实现爬取百度图片的方法。分享给大家供大家参考,具体如下:


import json
import itertools
import urllib
import requests
import os
import re
import sys
word=input("请输入关键字:")
path="./ok"
if not os.path.exists(path):
 os.mkdir(path)
word=urllib.parse.quote(word)
url = r"http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&queryWord={word}&cl=2&lm=-1&ie=utf-8&oe=utf-8&st=-1&ic=0&word={word}&face=0&istype=2nc=1&pn={pn}&rn=60"
urls=(url.format(word=word,pn=x)for x in itertools.count(start=0,step=60))
index=0
str_table = {
 '_z2C$q': ':',
 '_z&e3B': '.',
 'AzdH3F': '/'
}
char_table = {
 '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'
}
i=1
char_table = {ord(key): ord(value) for key, value in char_table.items()}
for url in urls:
 html=requests.get(url,timeout=10).text
 a=re.compile(r'"objURL":"(.*?)"')
 downURL=re.findall(a,html)
 for t in downURL:
   for key, value in str_table.items():
       t = t.replace(key, value)
   t=t.translate(char_table)
   try:
     html_1=requests.get(t)
     if str(html_1.status_code)[0]=="4":
       print('失败1')
       continue
   except Exception as e:
     print('失败2')
     continue
   with open(path+"/"+str(i)+".jpg",'wb') as f:
     f.write(html_1.content)
   i=i+1

希望本文所述对大家Python程序设计有所帮助。

来源:https://blog.51cto.com/11623741/2093582

标签:python,爬取,百度图片
0
投稿

猜你喜欢

  • Python卸载模块的方法汇总

    2022-03-14 16:50:21
  • 深入了解Django中间件及其方法

    2022-04-22 09:19:10
  • Python常用模块sys,os,time,random功能与用法实例分析

    2023-07-16 12:50:57
  • 使用Python脚本将文字转换为图片的实例分享

    2022-07-20 14:33:29
  • yolov5中train.py代码注释详解与使用教程

    2022-12-04 00:55:03
  • Python中属性和描述符的正确使用

    2021-08-14 04:26:45
  • Python爬虫Xpath定位数据的两种方法

    2022-07-03 16:36:53
  • MySQL 通过索引优化含ORDER BY的语句

    2010-03-25 10:28:00
  • Javascript实现的鼠标经过时播放声音

    2010-05-18 20:03:00
  • Django框架模型简单介绍与使用分析

    2021-04-06 02:59:19
  • 五个小窍门帮你写出更好的CSS代码

    2009-06-09 12:42:00
  • 归纳整理Python中的控制流语句的知识点

    2021-03-04 12:07:12
  • css中如何使div居中(垂直水平居中)

    2007-08-13 08:17:00
  • python中pycurl库的用法实例

    2021-05-21 10:10:23
  • pytorch 准备、训练和测试自己的图片数据的方法

    2021-02-27 13:54:35
  • Python字符串对齐、删除字符串不需要的内容以及格式化打印字符

    2021-09-17 10:46:33
  • 图解Python中浅拷贝copy()和深拷贝deepcopy()的区别

    2021-12-09 21:09:39
  • PHP实现将浏览历史页面网址保存到cookie的方法

    2023-08-16 05:25:29
  • 如何用Python合并lmdb文件

    2023-08-05 17:42:01
  • numpy 实现返回指定行的指定元素的位置索引

    2021-01-19 04:32:27
  • asp之家 网络编程 m.aspxhome.com