主要用到了多进程和多线程的知识,最后结果保存成csv文件格式,如有需要可改成数据库版本。

对用到的库做下简要介绍,具体请参考官方文档:

  1. xpinyin.Pinyin:将输入的中文转成拼音
  2. concurrent.futures.ProcessPoolExecutor:多进程
  3. concurrent.futures.ThreadPoolExecutor:多线程
# -*- coding: utf-8 -*-
# @Author: Studog
# @Date: 2017/5/24 9:27 import requests
import lxml.html as HTML
import csv
from xpinyin import Pinyin
import os
import concurrent.futures class GanjiSpider(object): def __init__(self):
self.city = input("请输入城市名:\n")
p = Pinyin()
city_name = p.get_initials(self.city, '').lower()
self.url = 'http://{0}.ganji.com/v/zhaopinxinxi/p1/'.format(city_name)
self.save_path = r'E:\data\ganji.csv'
file_dir = os.path.split(self.save_path)[0]
if not os.path.isdir(file_dir):
os.makedirs(file_dir)
if not os.path.exists(self.save_path):
os.system(r'echo > %s' % self.save_path) def get_job(self):
flag = True
with open(self.save_path, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['职位名称', '月薪', '最低学历', '工作经验', '年龄', '招聘人数','工作地点'])
while flag:
html = HTML.fromstring(requests.get(self.url).text)
content = html.xpath("//li[@class='fieldulli']/a/@href")
next_page = html.xpath("//li/a[@class='next']/@href")
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(self.get_url, content)
if next_page:
self.url = next_page[0]
else:
flag = False def get_url(self, html_page):
html = HTML.fromstring(requests.get(html_page).text)
job_list = html.xpath("//dl[@class='job-list clearfix']/dt/a/@href")
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(self.get_info, job_list) def get_info(self, job_url):
html = HTML.fromstring(requests.get(job_url).text)
name = html.xpath("//li[@class='fl']/em/a/text()")
info = html.xpath("//li[@class='fl']/em/text()")[1:]
address = html.xpath(("//li[@class='fl w-auto']/em//text()"))
if name and len(info) == 5 and address:
info[2] = info[2].strip()
address[2] = address[2].strip()
address = ''.join(address)
info.append(address)
name.extend(info)
print(name)
with open(self.save_path, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(name) if __name__ == '__main__':
gj = GanjiSpider()
gj.get_job()
 

利用python在windows环境下爬取赶集网工作信息。的更多相关文章

  1. Python 爬取赶集网租房信息

    代码已久,有可能需要调整 #coding:utf-8 from bs4 import BeautifulSoup #有这个bs4不用正则也可以定位要爬取的内容了 from urlparse impor ...

  2. Python爬虫基础--分布式爬取贝壳网房屋信息(Client)

    1. client_code01 2. client_code02 3. 这个时候运行多个client就可以分布式进行数据爬取.

  3. Python爬虫基础--分布式爬取贝壳网房屋信息(Server)

    1. server_code01 2. server_code02 3. server_code03

  4. 利用Python编写Windows恶意代码!自娱自乐!勿用于非法用途!

    本文主要展示的是通过使用python和PyInstaller来构建恶意软件的一些poc. 利用Python编写Windows恶意代码!自娱自乐!勿用于非法用途!众所周知的,恶意软件如果影响到了他人的生 ...

  5. 如何利用Xpath抓取京东网商品信息

    前几小编分别利用Python正则表达式和BeautifulSoup爬取了京东网商品信息,今天小编利用Xpath来为大家演示一下如何实现京东商品信息的精准匹配~~ HTML文件其实就是由一组尖括号构成的 ...

  6. Python爬虫项目--爬取自如网房源信息

    本次爬取自如网房源信息所用到的知识点: 1. requests get请求 2. lxml解析html 3. Xpath 4. MongoDB存储 正文 1.分析目标站点 1. url: http:/ ...

  7. Node.js爬虫-爬取慕课网课程信息

    第一次学习Node.js爬虫,所以这时一个简单的爬虫,Node.js的好处就是可以并发的执行 这个爬虫主要就是获取慕课网的课程信息,并把获得的信息存储到一个文件中,其中要用到cheerio库,它可以让 ...

  8. python 基于windows环境的ftp功能

    描述: 1.基于备份服务器部署的py程序,将需要备份主机目录下的内容下载至备份服务器(服务端和远端都是windows server 2008) 2.py程序部署在windows服务器,后台运行,基于b ...

  9. python添加Windows环境变量

    1.cmd中添加方式 SET PATH=%PATH%;c:\Program Files (x86)\Wireshark 注:如上代码添加c:\Program Files (x86)\Wireshark ...

随机推荐

  1. POJ 3020——Antenna Placement——————【 最小路径覆盖、奇偶性建图】

    Antenna Placement Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  2. winfrom C#树勾选等

    AfterCheck /// <summary> /// 树勾选 /// </summary> /// <param name="sender"> ...

  3. C# 获取格式

    1. 身份证日期 DateTime.ParseExact((IDCard.Substring(6, 8)), "yyyyMMdd", System.Globalization.Cu ...

  4. 一个ActionResult中定位到两个视图—<团委项目>

    在使用MVC做项目的时候一般的情况就是一个ActionResult一个视图,这样对应的Return View();就可以找到下面对应的视图,这是根据一个原则,“约定大于配置”,但是我们有的时候需要在一 ...

  5. 表单(form)成为 ajax 提交的表单(form)

    1.form <form id="ff" method="post"> <div> <label for="name&q ...

  6. ToDictionary写法

    把List集合转化成Dictionary public ActionResult Dimo() { Dictionary<string, Object> param = new Dicti ...

  7. spring笔记4-事务管理

    一.xml配置文件形式 通过转账案例,学习事务管理 1.建立数据库 2.编写entity package huguangqin.com.cnblogs.entity; public class Use ...

  8. canvas绘制圆环

  9. Python中open文件的各种打开模式

    对于Python打开文件的模式,总是记不住,这次在博客里记录一下 r+: Open for reading and writing.  The stream is positioned  at  th ...

  10. Linux centos6.x 配置免密码登录

    免密码登录主要就是被访机器提供公匙给访问者,然后访问者使用ssh协议时可以使用所配置好的公匙验证.这样就免去了输入密码的麻烦. 某些集群例如hadoop,一般都需要将主机和其他机器间配置无密码公匙认证 ...