Python crawls Baidu beauty pictures

PHPz
Release: 2017-04-04 10:34:21
Original
3271 people have browsed it

Through pythonAutomatically download Baidu beauties in batchesPictures

The effect is like this

Python crawls Baidu beauty pictures

##Effect display

My code

import requests
import re
Copy after login

Python crawls Baidu beauty pictures

Turn page to view picture mode

#获取每页图片的访问链接
def get_page():
    urls=['http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=%E7%BE%8E%E5%A5%B3%E5%9B%BE%E7%89%87&pn={}&gsm=3c00000000003c'.format(num) for num in range(0,20000,20)]
    for url in urls:
        print(url)
        get_img_link(url)
Copy after login
#从网页中获取每个图片的访问链接
def get_img_link(url):
    r=requests.get(url)
    #print(r.encoding)
    r.encoding='utf-8'
    html_code=r.text
    reg=re.compile(r'"objURL":"(.*?)"')
    imgs=re.findall(reg,html_code)
    # print(imgs)
    for img in imgs:
        #print(img)
        down_img(img)
Copy after login
#图片下载保存再本地
def down_img(url):
    web_data=requests.get(url)
    filename=url.split('/')[-1]
    targetfile='E:/pict_baidu/{}'.format(filename)
    with open(targetfile,'wb') as f:
        f.write(web_data.content)
Copy after login
if name=='main':
    get_page()
Copy after login
Summary

  • Crawler idea-Get multi-page access link->Get picture link for each page-> Image download;

  • The use of regular expressions;

  • The use of format and with open as syntax;

  • Adjustment of encoding method;

  • ##Usage of requests and re modules
  • .

The above is the detailed content of Python crawls Baidu beauty pictures. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template