python3.7 根据关键词 爬取百度图片并下载到本地

URL地址:http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1559725683677_R&pv=&ic=0&nc=1&z=&hd=&latest=©right=&se=1&showtab=0&fb=0&width=0&height=0&face=0&istype=2&ie=utf-8&ctd=1559725683679%5E00_1349X656&sid=&word=%E7%8C%AB
实现代码:

import re
import sys
import urllib
import requests
 
 
 
def getPage(keyword,page,n):
    page=page*n
    keyword=urllib.parse.quote(keyword, safe='/')
    url_begin= "http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word="
    url = url_begin+ keyword + "&pn=" +str(page) + "&gsm="+str(hex(page))+"&ct=&ic=0&lm=-1&width=0&height=0"
    return url
def get_onepage_urls(onepageurl):
    try:
        html = requests.get(onepageurl).text
        print=(html)
    except Exception as e:
        print(e)
        pic_urls = []#列表形式的URL
        return pic_urls
    pic_urls = re.findall('"objURL":"(.*?)",', html, re.S)
    return pic_urls
 
 
def down_pic(pic_urls):
    """给出图片链接列表, 下载所有图片"""
    for i, pic_url in enumerate(pic_urls):
        try:
            pic = requests.get(pic_url, timeout=15)
            string =str(i + 1) + '.jpg'
            with open(string, 'wb') as f:
                f.write(pic.content)
                print('成功下载第%s张图片: %s' % (str(i + 1), str(pic_url)))
        except Exception as e:
            print('下载第%s张图片时失败: %s' % (str(i + 1), str(pic_url)))
            print(e)
            continue
 
 
if __name__ == '__main__':
    keyword = '猫'  # 关键词, 改为你想输入的词即可, 相当于在百度图片里搜索一样
    page_begin=0
    page_number=30
    image_number=3
    all_pic_urls = []
    while 1:
        if page_begin>image_number:
            break
        print("第%d次请求数据",[page_begin])
        url=getPage(keyword,page_begin,page_number)
        onepage_urls= get_onepage_urls(url)
        page_begin += 1
 
        all_pic_urls.extend(onepage_urls)
 
    down_pic(list(set(all_pic_urls)))

实现图:
python3.7 根据关键词 爬取百度图片并下载到本地_第1张图片

你可能感兴趣的:(Python)