1、导入包
import requests #取数
from lxml import etree #用xpath解析
import pymysql #连接数据库
import chardet #自动获取编码
2、获取单页html
def get_one_page(url):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36'}
response = requests.get(url, headers=headers) #习惯先把头部信息加上
response.encoding = chardet.detect(response.content)['encoding'] #用chardet.detect方法自动获取网页的编码,也可以自己手动在网页查
return response.text
3、解析html
def parse_one_page(html):
#对获取内容初始化,再用parse函数etree.HTML解析
result = etree.HTML(html)
item = {} #建立一个字典储存所有职位信息
item['t1'] = result.xpath('//div[@class="el"]/p/span/a/text()') #职位名称
item['t2'] = result.xpath('//div[@class="el"]/span[@class="t2"]/a/text()') #公司名称
item['t3'] = result.xpath('//div[@class="el"]/span[@class="t3"]/text()') #工作地点
t4 = result.xpath('//div[@class="el"]/span[@class="t4"]') #text无法获取空值(薪资数据可能为空),所以要用string方法获取
item['t4'] = []
for i in t4:
item['t4'].append(i.xpath('string(.)')) #遍历出来再用xpath解析,string(.)中间的点表示在当前目录
item['t5'] = result.xpath('//div[@class="el"]/span[@class="t5"]/text()') #发布时间
item['href'] = result.xpath('//div[@class="el"]/p/span/a/@href') #详细链接
4、数据清洗
上面第3步将数取出,存在字典里,接下来做数据清洗,这部分还是在parse_one_page函数体里。

# (1) 去掉每个职位名称前后空白
for i in range(len(item['t1'])): #有多少个职位就遍历多少遍
item['t1'][i] = item['t1'][i].strip() #strip只针对字符串
# (2) 薪资处理
# 定义列表,存储处理后的薪资数据
sal_low = [] #最低月薪
sal_height = [] #最高月薪
for sal in item['t4']: #取出的是字符串
if sal != "": #如果薪资不为空,则先截取
sal = sal.strip().split('-') #将薪资分成两部分
if len(sal) > 1: #若长度>1,则说明薪资是个区间,有最大最小值
#研究薪资结构,一般是万/月,千/月,万/年,其它的设为0值
if sal[1][-3] == '万' and sal[1][-1] == '月': #判断第二部分的构成
sal_low.append(float(sal[0])*10000) #float设置成浮点数
sal_height.append(float(sal[1][0:-3])*10000)
elif sal[1][-3] == '万' and sal[1][-1] == '年':
sal_low.append(round(float(sal[0])*10000/12,1)) #round保留一位小数,月薪=年薪/12
sal_height.append(round(float(sal[1][0:-3])*10000/12,1))
elif sal[1][-3] == '千' and sal[1][-1] == '月':
sal_low.append(float(sal[0])*1000)
sal_height.append(float(sal[1][0:-3])*1000)
else:
sal_low.append(0) #若存在其它情况则全部设为0
sal_height.append(0)
else:
#否则,薪资只有一个固定值
if sal[0][-3] == '元' and sal[0][-1] == '天':
sal_low.append(sal[0][0:-3]) #直接把数字填进去(日薪)
sal_height.append(sal[0][0:-3]) #因为只有一个值,所以最低最高薪资是相同的
else:
sal_low.append(0)
sal_height.append(0)
else: #若为空
sal_low.append(0)
sal_height.append(0)
# 将处理后的薪资存储在字典中
item['sal_low'] = sal_low
item['sal_height'] = sal_height

# (3) 时间数据处理
for i in range(len(item['t5'])):
item['t5'][i] = '2019-' + item['t5'][i] # 遍历出来把每个结果前面都加上年份

yield item
5、存储至mysql
def write_to_mysql(content):
# 建立连接
conn = pymysql.connect(host='localhost',user='root',passwd='vicky',db='test_db',charset='utf8')
cursor = conn.cursor()
for i in range(len(content['t1'])):
# 在这里只取了下面7个字段
jobname = content['t1'][i]
company = content['t2'][i]
workplace = content['t3'][i]
salary_low = content['sal_low'][i]
salary_height = content['sal_height'][i]
ptime = content['t5'][i]
href = content['href'][i]
# 在这一步的时候可以去Navicat创建一张表,字段可以多加一个id为主键自增
sql = "insert into wuyoujob values(null,%s,%s,%s,%s,%s,%s,%s)"
parm = (jobname,company,workplace,salary_low,salary_height,ptime,href)
cursor.execute(sql,parm)
conn.commit()
cursor.close()
conn.close(http://www.my516.com)
5、函数回调
函数写好了,实例化就行

def main(page):
url = 'https://search.51job.com/list/080200,000000,0000,00,9,99,%25E6%2595%25B0%25E6%258D%25AE%25E5%2588%2586%25E6%259E%2590%25E5%25B8%2588,2,'+str(page)+'.html?' #这里要注意,原地址中间?后面的内容都可以删掉,取前面就好,做个分页时注意要转成字符串格式才能拼接
html = get_one_page(url)
for i in parse_one_page(html): #遍历字典
print(i) #打印处理后的数据(字典)也可以不打印
write_to_mysql(i) #把字典的内容传给数据库
6、回调主函数,完成分页
if __name__ == '__main__':
for i in range(1,9): #这里看自己抓取的网页大概有多少页
main(i)
然后打开Navicat,刷新一下表,见证奇迹的时候到了!

Python3爬取前程无忧数据分析工作并存储到MySQL的更多相关文章

  1. Python爬取招聘信息,并且存储到MySQL数据库中

    前面一篇文章主要讲述,如何通过Python爬取招聘信息,且爬取的日期为前一天的,同时将爬取的内容保存到数据库中:这篇文章主要讲述如何将python文件压缩成exe可执行文件,供后面的操作. 这系列文章 ...

  2. 爬取豆瓣电影top250并存储到mysql数据库

    import requests from lxml import etree import re import pymysql import time conn= pymysql.connect(ho ...

  3. Python3爬取人人网(校内网)个人照片及朋友照片,并一键下载到本地~~~附源代码

    题记: 11月14日早晨8点,人人网发布公告,宣布人人公司将人人网社交平台业务相关资产以2000万美元的现金加4000万美元的股票对价出售予北京多牛传媒,自此,人人公司将专注于境内的二手车业务和在美国 ...

  4. python3爬取网页

    爬虫 python3爬取网页资源方式(1.最简单: import'http://www.baidu.com/'print2.通过request import'http://www.baidu.com' ...

  5. python3爬取女神图片,破解盗链问题

    title: python3爬取女神图片,破解盗链问题 date: 2018-04-22 08:26:00 tags: [python3,美女,图片抓取,爬虫, 盗链] comments: true ...

  6. Python3 爬取微信好友基本信息,并进行数据清洗

    Python3 爬取微信好友基本信息,并进行数据清洗 1,登录获取好友基础信息: 好友的获取方法为get_friends,将会返回完整的好友列表. 其中每个好友为一个字典 列表的第一项为本人的账号信息 ...

  7. python3爬取微博评论并存为xlsx

    python3爬取微博评论并存为xlsx**由于微博电脑端的网页版页面比较复杂,我们可以访问手机端的微博网站,网址为:https://m.weibo.cn/一.访问微博网站,找到热门推荐链接我们打开微 ...

  8. python3爬取全民K歌

    Python3爬取全民k歌 环境 python3.5 + requests 1.通过歌曲主页链接爬取 首先打开歌曲主页,打开开发者工具(F12). 选择Network,点击播放,会发现有一个请求返回的 ...

  9. Python3爬取猫眼电影信息

    Python3爬取猫眼电影信息 import json import requests from requests.exceptions import RequestException import ...

随机推荐

  1. 可恢复的安全rm

    我们常常使用rm去删除一些文件.假设不小手一抖,那么就悲剧了.你们都懂的... 在经历过一次这种慘剧后.决定永远杜绝这种情况.重写写了shell函数.运行安全的rm.这个函数会把要删除的文件按日期备份 ...

  2. 用XMLRPC开服务进行server/client通信

    本文讲一下怎样用python的xmlrpc开服务,进行server/client的通信. 应用场景:1)需多client訪问应用程序给予应答情况--网页服务.  2)数据极大,希望载入一次.后面仅仅用 ...

  3. 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取

    装饰者模式的学习(c#) 案例转自https://www.cnblogs.com/stonefeng/p/5679638.html //主体基类 using System;using System.C ...

  4. 工作总结 c#如何将两个List集合合并

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. NIO框架之MINA源代码解析(二):mina核心引擎

    NIO框架之MINA源代码解析(一):背景 MINA的底层还是利用了jdk提供了nio功能,mina仅仅是对nio进行封装.包含MINA用的线程池都是jdk直接提供的. MINA的server端主要有 ...

  6. libcurl库进行http通讯-一些主要的函数

    这里就简介一下libcurl的一些主要的函数. 调用curl_global_init()初始化libcurl 调用curl_easy_init()函数得到 easy interface型指针 调用cu ...

  7. java(JSP)中几种获取项目路径方式

    在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getPro ...

  8. LNMP环境搭建——PHP篇

    一.源代码安装 1.编译安装 ./configure --prefix=/usr/local/php\ --with-config-file-path=/usr/local/php/etc --wit ...

  9. opencv中RGB转HSV

    cvCvtColor(src,dst,CV_BGR2HSV); 当中,src为三通道的,dst也为三通道的. OPENCV 中 H.S.V.顺序分别为3*x+0  3*x+1   3*x+2 open ...

  10. c++中读写文件操作

    读写文件这个,不常用,每次用的时候都会百度一下,每次写法还都不一样,所有总是记混.今天利用点时间总结下之前工程中用过的.以后就安照这种方法写了. 搞acmicpc的时候喜欢用freopen(),这个是 ...