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

  

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

        进口json   出现进口itertools   进口urllib   进口的要求   进口操作系统   进口再保险   导入系统   词=输入(“请输入关键字:”)   路径=" ./ok”   如果不是os.path.exists(路径):   os.mkdir(路径)   词=urllib.parse.quote(单词)   url=r " http://image.baidu.com/search/acjson& # 63; tn=resultjson_com& ipn=rj& ct=201326592, fp=result& queryWord={词},cl=2, lm=1,即=utf-8& oe=utf-8&圣=1,ic=0,词={词},面对=0,数控=1=2的类型及pn={pn}, rn=60”   url=(url.format(=词,pn=x) x count(=0开始,一步=60))   指数=0   str_table={   “美元_z2C q”:“:”,   “_z& e3B”:“。”   “AzdH3F”:“/?   }   char_table={   ' w ': ' a ',   “k”:“b”,   “v”:“c”,   ' 1 ':' d ',   “j”:“e”,   “u”:“f”,   ' 2 ':' g ',   “我”:“h”,   “t”:“我”,   “3”:“j”,   “h”:“k”,   “年代”:“l”,   “4”:“m”,   ' g ': ' n ',   “5”:“o”,   “r”:“p”,   “问”:“问”,   “6”:“r”,   “f”:“年代”,   “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={奥德(关键):奥德(价值)键,价值char_table.items ()}   为在url网址:   html=requests.get (url,超时=10)。text   a=re.compile (r”objURL”:“(* & # 63;)”的)   downURL=re.findall (html)   t downURL:   关键,价值str_table.items ():   t=t。替换(关键字,值)   t=t.translate (char_table)   试一试:   html_1=requests.get (t)   如果str (html_1.status_code) [0]==?”:   打印(“失败1 ')   继续   除了例外e:   打印(“失败2”)   继续   打开(路径+ " + str (i) +“jpg”,“世界银行”)f:   f.write (html_1.content)   我+ 1=之前      

更多关于Python相关内容可查看本站专题:《Python套接字编程技巧总结》,《巨蟒正则表达式用法总结》,《Python数据结构与算法教程》、《Python函数使用技巧总结》,《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

  

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

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