import requests
from lxml import etree
url = 'https://aspx.sc.chinaz.com/query.aspx?keyword=%E5%85%8D%E8%B4%B9&issale=0&classID=864'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82'
}
# 对互联网页面获取
response = requests.get(url=url, headers=header).text
tree = etree.HTML(response)
# 第一次路径追踪。目标是局部爬虫获取该简历的url和名称
div_list = tree.xpath('//div[@class="box col3 ws_block"]')
for li in div_list:
a_name = li.xpath('./a/img/@alt')[0] + '.rar' # name同时也是下载路径
don_url = li.xpath('./p/a/@href') # url
# 特判处理一下
if don_url[0][0] != 'h':
don_url[0] = 'https:' + don_url[0]
# 第二次对单独简历页面获取
response2 = requests.get(url=don_url[0], headers=header).text
tree2 = etree.HTML(response2)
# 第二次路径追踪,获取下载地址链接
div_list2 = tree2.xpath('//div[@class="down_wrap"]/div[2]/ul/li[1]')
for li2 in div_list2:
# 得到下载链接
don_path = li2.xpath('./a/@href')[0]
# 对下下载链接响应
response3 = requests.get(url=don_path, headers=header).content
path = 'libs/' + a_name
with open(path, 'wb') as fp:
fp.write(response3)
print(a_name + '下载完成')
作业:爬取站长素材中的免费简历模板
于 2023-07-20 14:22:28 首次发布