分享一段百度爬虫代码

import requests
import re
import os
import time

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'}
name = '切割机'
num = 0
x =10 #input('您要爬取几张呢?,输入1等于60张图片。')
for i in range(int(x)):
    name_1 =r'D:\object\PycharmProjects\aiproess\speader\bauduword\imgs'#图片保存地址
    url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+name+'&pn='+str(i*30)
    res = requests.get(url,headers=headers)
    htlm_1 = res.content.decode()
    a = re.findall('"objURL":"(.*?)",',htlm_1)
    if not os.path.exists(name_1):
        os.makedirs(name_1)
    for b in a:
        num = num +1
        try:
            img = requests.get(b)
        except Exception as e:
            print('第'+str(num)+'张图片无法下载------------')
            print(str(e))
            continue
        f = open(name_1+'/'+name+str(num)+'.jpg','ab')
        print('---------正在下载第'+str(num)+'张图片----------')
        f.write(img.content)
        f.close()
print('下载完成')

电脑系统是windows10

<切割机>就是我们要爬取的关键字,图片保存地址注意按自己需要更改

注意别爬取太多,会被封的!

你可能感兴趣的:(爬虫,java,docker,python,c++,c语言,sublime,text)