对前程无忧的初步爬取

爬取的网页


image.png
import  requests
from lxml import etree
import json

url = "https://search.51job.com/list/010000,000000,0000,00,9,99,python,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="

里面有空格要处理下

header = {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding":"gzip, deflate, br",
"Accept-Language":"zh-CN,zh;q=0.9",
"Connection":"keep-alive",
"Host":"search.51job.com",
"Sec-Fetch-Mode":"navigate",
"Sec-Fetch-Site":"none",
"Upgrade-Insecure-Requests":"1",
"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"}

网页处理

response = requests.get(url=url,headers=header)
response.encoding = "gbk"
html_51job = etree.HTML(response.text)
all_div = html_51job.xpath("//div[@id='resultList']//div[@class='el']")
info_list = []
for item in all_div:
    info = {}
    # 这个非常重要,代表我们使用的是item下的xpath语句,不要把 . 丢了
    # 获取数据的时候,要使用列表索引为0的数据
    info['job_name'] = item.xpath("./p/span/a/@title")[0]
    info['company_name'] = item.xpath(".//span[@class='t2']/a/@title")[0]
    info['company_address'] = item.xpath(".//span[@class='t3']/text()")
    # money 字段可能为空, try, except来进行异常处理
    try:
        info['money'] = item.xpath(".//span[@class='t4']/text()")[0]
    except IndexError:
        info['money'] = '无数据'
    info['date'] = item.xpath(".//span[@class='t5']/text()")[0]
    info_list.append(info)
return info_list
print(json.dumps(info_list))
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • “当机立断”的重要性已为人们深知,机遇来临时果断决策,不能稍有犹豫,否则就会将已有的优势白白浪费掉。 唐朝时,河南...
    易流黄滨老师阅读 4,241评论 0 2
  • 鹅蛋(英文:Gooze egg),成椭圆形,个体很大,味道有些油,必须用很新鲜的鹅蛋稍加烹煮后食用。鹅蛋每颗约重2...
    f763b016cd49阅读 4,140评论 0 0
  • 长达一小时四十八分钟的影片,最触目的倒不是最后一幕女主倒在舞台的血泊里,而是中间部分那只有几十秒的一瞬:...
    木有枝啊哈哈阅读 3,345评论 2 1

友情链接更多精彩内容