吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6687|回复: 22
收起左侧

[Python 转载] 转帖--程序人生的py爬取智联招聘(基础版)

  [复制链接]
知心 发表于 2018-4-20 10:14
本帖最后由 知心 于 2018-4-24 14:18 编辑

[Python] 纯文本查看 复制代码
#-*- coding: utf-8 -*-
import re
import csv
import requests
from tqdm import tqdm
from urllib.parse import urlencode
from requests.exceptions import RequestException

def get_one_page(city, keyword, region, page):
   '''
   获取网页html内容并返回
   '''
   paras = {
       'jl': city,         # 搜索城市
       'kw': keyword,      # 搜索关键词 
       'isadv': 0,         # 是否打开更详细搜索选项
       'isfilter': 1,      # 是否对结果过滤
       'p': page,          # 页数
       're': region        # region的缩写,地区,2005代表海淀
   }

   headers = {
       'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
       'Host': 'sou.zhaopin.com',
       'Referer': 'https://www.zhaopin.com/',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
       'Accept-Encoding': 'gzip, deflate, br',
       'Accept-Language': 'zh-CN,zh;q=0.9'
   }

   url = 'https://sou.zhaopin.com/jobs/searchresult.ashx?' + urlencode(paras)
   try:
       # 获取网页内容,返回html数据
       response = requests.get(url, headers=headers)
       # 通过状态码判断是否获取成功
       if response.status_code == 200:
           return response.text
       return None
   except RequestException as e:
       return None

def parse_one_page(html):
   '''
   解析HTML代码,提取有用信息并返回
   '''
   # 正则表达式进行解析
   pattern = re.compile('<a style=.*? target="_blank">(.*?)</a>.*?'        # 匹配职位信息
       '<td class="gsmc"><a href="(.*?)" target="_blank">(.*?)</a>.*?'     # 匹配公司网址和公司名称
       '<td class="zwyx">(.*?)</td>', re.S)                                # 匹配月薪      

   # 匹配所有符合条件的内容
   items = re.findall(pattern, html)   

   for item in items:
       job_name = item[0]
       job_name = job_name.replace('<b>', '')
       job_name = job_name.replace('</b>', '')
       yield {
           'job': job_name,
           'website': item[1],
           'company': item[2],
           'salary': item[3]
       }

def write_csv_file(path, headers, rows):
   '''
   将表头和行写入csv文件
   '''
   # 加入encoding防止中文写入报错
   # newline参数防止每写入一行都多一个空行
   with open(path, 'a', encoding='gb18030', newline='') as f:
       f_csv = csv.DictWriter(f, headers)
       f_csv.writeheader()
       f_csv.writerows(rows)

def write_csv_headers(path, headers):
   '''
   写入表头
   '''
   with open(path, 'a', encoding='gb18030', newline='') as f:
       f_csv = csv.DictWriter(f, headers)
       f_csv.writeheader()

def write_csv_rows(path, headers, rows):
   '''
   写入行
   '''
   with open(path, 'a', encoding='gb18030', newline='') as f:
       f_csv = csv.DictWriter(f, headers)
       f_csv.writerows(rows)

def main(city, keyword, region, pages):
   '''
   主函数
   '''
   filename = 'zl_' + city + '_' + keyword + '.csv'
   headers = ['job', 'website', 'company', 'salary']
   write_csv_headers(filename, headers)
   for i in tqdm(range(pages)):
       '''
       获取该页中所有职位信息,写入csv文件
       '''
       jobs = []
       html = get_one_page(city, keyword, region, i)
       items = parse_one_page(html)
       for item in items:
           jobs.append(item)
       write_csv_rows(filename, headers, jobs)

if __name__ == '__main__':
   main('北京', 'python工程师', 2005, 10)
2018042010120585142.jpg
2018042010122182117.jpg

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
一牛神一 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

gufan8503 发表于 2018-4-27 08:44
我试了下,好像不行啊,咋回事额,新手来的
Traceback (most recent call last):
  File "/tmp/626646822/main.py", line 5, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

exit status 1
aozakiaoko 发表于 2018-4-27 11:03
gufan8503 发表于 2018-4-27 08:44
我试了下,好像不行啊,咋回事额,新手来的
Traceback (most recent call last):
  File "/tmp/626646822 ...

没装requests库
执行 pip install requests
之后再试
一牛神一 发表于 2018-4-20 10:19
vip537701314 发表于 2018-4-20 10:21
支持一波!!
Muter 发表于 2018-4-20 10:22
支持楼主~
dyb1987 发表于 2018-4-20 10:43
才开始学习python,虽然不能完全看懂,只能看懂3分一,看来还需要多多努力
46613952 发表于 2018-4-20 10:45
好厉害了
会飞的胖胖 发表于 2018-4-20 10:55
刚在微信公总号看到
爱挠头 发表于 2018-4-20 11:02
能不能爬58的
上官轩墨 发表于 2018-4-20 11:17
感谢楼主分享
 楼主| 知心 发表于 2018-4-20 11:32
会飞的胖胖 发表于 2018-4-20 10:55
刚在微信公总号看到

嗯嗯,分享给更多需要的人
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 10:24

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表