python写爬虫

import requests
import re
import os
headers={'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
res=requests.get('https://www.vmgirls.com/12945.html',headers=headers)
html=res.text
dir_name=re.findall('<h1 class="post-title h3">(.*?)</h1>',html)[-1]
#print(dir_name)
if not os.path.exists(dir_name):
   os.mkdir(dir_name)

urls=re.findall('<img alt=".*?" src=".*?" data-src="(.*?)" data-nclazyload="true">',html)

for url in urls:
    #time.sleep(1)
    file_name=url.split('/')[-1]
    res1=requests.get(url,headers=headers)
    with open(dir_name+'/'+file_name,'wb') as f:
        f.write(res1.content)

 

©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页