转帖--程序人生的py爬取智联招聘(基础版)
本帖最后由 知心 于 2018-4-24 14:18 编辑#-*- 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
job_name = job_name.replace('<b>', '')
job_name = job_name.replace('</b>', '')
yield {
'job': job_name,
'website': item,
'company': item,
'salary': item
}
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) 我试了下,好像不行啊,咋回事额,新手来的
Traceback (most recent call last):
File "/tmp/626646822/main.py", line 5, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
exit status 1 gufan8503 发表于 2018-4-27 08:44
我试了下,好像不行啊,咋回事额,新手来的
Traceback (most recent call last):
File "/tmp/626646822 ...
没装requests库
执行 pip install requests
之后再试 虽然看不太懂,但还是要支持! 支持一波!! 支持楼主~ 才开始学习python,虽然不能完全看懂,只能看懂3分一,看来还需要多多努力 好厉害了 刚在微信公总号看到:Dweeqw 能不能爬58的 感谢楼主分享 会飞的胖胖 发表于 2018-4-20 10:55
刚在微信公总号看到
嗯嗯,分享给更多需要的人