首页 > 代码库 > python写的百度图片爬虫

python写的百度图片爬虫

学了一下python正则表达式,写一个百度图片爬虫玩玩。

当技术遇上心术不正的人,就成我这样的2B青年了。

python3.6开发,在Windows下需要安装vc2015动态库。下载地址:http://www.cr173.com/soft/146014.html

 

 1 #/usr/bin/env python
 2 #Guoyabin
 3 #-*- coding:utf-8 -*-
 4 import re,os
 5 import requests
 6 
 7 keyword=input(请输入搜索关键词:)
 8 os.chdir(C:\\Users\\Administrator\\Desktop\\)
 9 if os.path.exists(keyword) ==False:
10     os.mkdir(keyword)
11 os.chdir(keyword)
12 
13 page_begin=input(从第几页开始搜索,回车从第一页开始:)
14 if len(page_begin)==0:
15     baidupn=0
16 else:
17     baidupn=page_begin*20
18 
19 num=1
20 for i in range(50):
21     url=https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8
22     payload={word:keyword,pn:baidupn}
23     html = requests.get(url,params=payload).text
24     regular="objURL":"(.*?)",
25     pic=re.findall(regular,html)
26     baidupn+=20
27 
28     for tu in pic:
29         try:
30             dl=requests.get(tu,timeout=60)
31             #pic_name=os.path.basename(tu).split(‘?‘)[0]
32             pic_name=%s%s %(num,.jpg)
33             with open(pic_name,"wb") as code:
34                 code.write(dl.content)
35                 requests.session().keep_alive = False
36                 dl.close()
37             num+=1
38         except requests.exceptions.ConnectionError:
39             print(这张图片下载失败了,图片地址,tu)
40             continue

 

 

无耻的求一下赞助

技术分享技术分享

python写的百度图片爬虫