注册 登录
编程论坛 Python论坛

python-简单的图片爬虫(关键字搜索爬虫)

廉价的咖啡 发布于 2016-10-04 23:27, 2958 次点击
#coding=utf-8

import requests
import re

#guan jian zi sou suo
word = raw_input("Input key word(抱歉!不支持关键字中文输入):")
url = 'http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1475592806271_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word='+word+'#z=0&pn=&ic=0&st=-1&face=0&s=0&lm=-1'
result = requests.get(url)

#huo qu wang ye yuan dai ma
html = requests.get(url).text
pic_url = re.findall('"objURL":"(.*?)",',html,re.S)
i = 0

#yi ci yuan cheng xia zai
for each in pic_url:
    print each
    try:
        pic = requests.get(each,timeout = 10)
    except requests.exceptions.ConnectionError:
        print'【错误】当前图片无法下载'
        continue
    string = 'E:\XIXI\\' +str(i)+'.jpg'
    fp = open(string,'wb')
    fp.write(pic.content)
    fp.close()
    i = i+1
0 回复
1