You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import requests
from lxml import etree
f=open('advertise.txt','w',encoding='utf-8')
#url='https://www.job001.cn/jobs?pageNo=1'
def run(url):
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62'}
r=requests.get(url,headers=headers)
r.encoding='utf-8'
html=r.text
doc=etree.HTML(html) #解析requests返回的响应结果
# 通过 xpath 定位所有的 div 标签xpath 方法返回的是一个列表
divs=doc.xpath('/html/body/div[15]/div[1]/div[1]/div[@class="jobsList"]')
# 我们获取到的是一个包含所有 tr 的列表,因此我们需要遍历该列表
for div in divs:
zhiwei = div.xpath('.//div[@class="mouseListenTop clearfix"]//a/text()')[0].strip()
gongsi = div.xpath('.//div[@class="jobRight"]//a/text()')[0].strip() #XPath 中的 text() 可以获取文本信息,由于返回的是一个列表,所以我们直接提取列表的第一个属性即可
yuexin = div.xpath('.//span[@class="salaryList"]/text()')[0].strip()
diqu = div.xpath('.//span[@class="cityConJobsWork"]/text()')[0].strip()
riqi = div.xpath('.//span[@class="time"]/text()')[0].strip()
#将信息写入文件
f.write(zhiwei+'|'+gongsi+'|'+yuexin+'|'+diqu+'|'+riqi+'\n')
for i in range(1,3):
url='https://www.job001.cn/jobs?pageNo='+str(i)
run(url)
f.close()
print("over!")