Python teaches you to climb "pictures" online

Python teaches you to crawl "pictures" online

import requests
import re
import time
import os

# url = 'https://www.vmgirls.com/13344.html'

def get_pic(url):
    headers = {
    
    
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }

    response = requests.get(url, headers=headers)
    html = response.text
    # print(response.text)

    dir_name = re.findall('<h1 class="post-title h1">(.*?)</h1>',html)[-1]
    if not os.path.exists(dir_name):
        os.mkdir(dir_name)

    urls = re.findall('<a href="(.*?)" alt=".*?" title=".*?">', html)
    for url_data in urls:
        # time.sleep(1)
        urls_data = "https:" + url_data
        print(urls_data)
        # 图片的名称
        file_name = urls_data.split('/')[-1]
        # print(file_name)
        response_data = requests.get(urls_data, headers = headers)
        # print(response_data.content)
        with open(dir_name + '/' + file_name, 'wb') as f:
            f.write(response_data.content)

url_list =[
    'https://www.vmgirls.com/13344.html',
    'https://www.vmgirls.com/15881.html',
    'https://www.vmgirls.com/15400.html',
    'https://www.vmgirls.com/15323.html',
    'https://www.vmgirls.com/15370.html'

]

for url1 in url_list:
    get_pic(url1)

After the crawling is complete, use the browser to open the picture!!!

Guess you like

Origin blog.csdn.net/HYXRX/article/details/114929370