爬取简历网站所有简历模板到本地

from lxml import etree
import requests
import os
if __name__  == "__main__" :
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.46'
    }
    #简历网站url地址
    url = "https://sc.chinaz.com/jianli/free.html"
    #请求响应
    page_text = requests.get(url=url,headers=header).content
    #将网页html加载到etree对象中
    tree = etree.HTML(page_text)
    #获取简历的a标签
    host_li_list = tree.xpath('//div[@id="main"]/div/div/a')
    #如果jianlirar文件不存在,自动创建该文件
    if not os.path.exists('./jianlirar'):
        os.mkdir('./jianlirar')
    for i in host_li_list:
        #获取a标签中href的地址请求响应
        src = requests.get(url=i.xpath('./@href')[0],headers=header).text
        #加载响应后的网页文档到etree中
        new_html = etree.HTML(src)
        #获取新网页的下载地址
        new_src = new_html.xpath('//div[@class="down_wrap"]//ul/li/a/@href')[0]
        #请求下载地址
        path = requests.get(url=new_src,headers=header).content
        #name_list是每一个简历完整的文件名路径
        name_list = './jianlirar/'+i.xpath('./img/@alt')[0]+".rar"
        with open(name_list,'wb') as f :
            #依次将请求path写入到文件中,文件格式为rar
            f.write(path)
            print(name_list+"下载成功!")

 

posted @ 2023-10-22 22:16  敲代码的PinkPig  阅读(64)  评论(0编辑  收藏  举报