您好,欢迎来到尚车旅游网。
搜索
您的当前位置:首页利用python爬取58同城简历数据

利用python爬取58同城简历数据

来源:尚车旅游网
利⽤python爬取58同城简历数据

需要的python包urllib2,beautifulSoup,MySQLdb,re第⼀,获取整个页⾯

coding:utf-8

⽐如,我们需要获取姓名

通过控制台可以看到名字所在的位置这⾥写图⽚描述

可⽤正则表达式进⾏匹配,代码如下:

name = re.findall(r'(?<=class=\"name\">).*?(?=)',str(soup))1

运⾏程序,发现返回结果为空。

检查正则表达式是⽆误的,我们观察之前返回的soup,发现他返回的源码与⽹页上的源码是不⼀样的。所有我们根据观察⽹页上的源码写的正则表达式不能再返回的源码中匹配到相应的内容。因此我们只能通过观察返回的源码写正则表达式。这⾥写图⽚描述

在soup返回的源码中,我们很容易地找到这个⼈的全部基本资料,⽽且都在标签为< li class=”item”>中,通过下⾯的fandAll()⽅法,很容易就获取内容

data = soup.findAll('li',attrs={'class':'item'})1

通过上⾯的代码,可以的到如下的结果,可见返回了⼀个list这⾥写图⽚描述

这样,我们就获取了这个⼈的姓名,性别,年龄,⼯作经验和学历。通过上⾯的⽅法,我们能够获取整个页⾯你所需要的数据。第三,把数据保存到数据库

我使⽤的是mysql数据库,所以这⾥以mysql为例连接数据库

conn = MySQLdb.Connect(host = '127.0.0.1',

port = 3306,user = 'root',passwd = 'XXXXX',db = 'XXXXX',charset = 'utf8')

cursor = conn.cursor()1234567

因为要存储中⽂,所以在这⾥设置编码格式为utf8

创建插⼊语句

sql_insert = \"insert into resume(

ID,name,sex,age,experience,education,pay,ad,job,job_experience,education_experience)

values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)\"1234

插⼊数据

cursor.execute(sql_insert,(ID,name,sex,age,experience,education,pay,ad,job,job_experience,education_experience))

conn.commit()123

关闭数据库cursor.close()conn.close()12

执⾏程序报错了…

(1064, \"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to usenear '))' at line 1\")1

发⽣这个错误,如果sql语法没错,⼀般就是编码有问题了。

我们的数据库使⽤的编码是utf8,应该是插⼊的数据在编码上出现问题了。我们对返回的数据进⾏重新编码⽤decode()和encode()⽅法实现

name = data[0].decode('utf-8').encode('utf-8')1

⽤这个简单的⽅法,我们就解决了数据库编码与数据编码不⼀致导致出错的问题。

为什么编码会不⼀样呢?

这是因为,我们⽤BeautifulSoup包爬取⽹页的时候,返回的数据是ascii编码的数据。⽽我们的数据库为utf8编码的,所有插⼊数据是会发⽣错误,只要对爬取的数据重新进⾏编码结果

这⾥写图⽚描述

这个是我爬取的结果,效果还是挺好的,速度⼤概是1秒个⽹页,虽然⽐起scrapy要慢好多,但是BeautifulSoup和urllib2使⽤简单,适合新⼿练⼿。附录:代码

coding:utf-8

import urllib2

from BeautifulSoup import BeautifulSoupimport re

import MySQLdb

url = 'http://jianli.58.com/resume/91655325401100'content = urllib2.urlopen(url).read()soup = BeautifulSoup(content)

basedata = str(soup.findAll('li',attrs={'class':'item'}))

basedata = re.findall(r'(?<=class=\"item\">).?(?=)',basedata)ID = str(soup.findAll('script',attrs={'type':'text/javascript'}))ID = re.findall(r'(?<=global.ids = \").?(?=\"󰀀',ID)ID = ID[0].decode('utf-8').encode('utf-8')

name = basedata[0].decode('utf-8').encode('utf-8')sex = basedata[1].decode('utf-8').encode('utf-8')age = basedata[2].decode('utf-8').encode('utf-8')

experience = basedata[3].decode('utf-8').encode('utf-8')education = basedata[4].decode('utf-8').encode('utf-8')pay = str(soup.findAll('dd',attrs={None:None}))pay = re.findall(r'(?<=

)\\d+.?(?=)',pay)

pay = pay[0].decode('utf-8').encode('utf-8')

expectdata = str(soup.findAll('dd',attrs={None:None}))expectdata = re.findall(r'''(?<=[\"']>)[^<].?(?=)''',expectdata)ad = expectdata[0].decode('utf-8').encode('utf-8')job = expectdata[1].decode('utf-8').encode('utf-8')

job_experience = str(soup.findAll('div',attrs={'class':'employed'}))job_experience = re.findall(r'(?<=>)[^<].?(?=<)',job_experience)

job_experience = ''.join(job_experience).decode('utf-8').encode('utf-8')education_experience = str(soup.findAll('dd',attrs={None:None}))

education_experience = re.findall(r'(?<=

).\\n.\\n?.',education_experience)

education_experience = ''.join(education_experience).decode('utf-8').encode('utf-8')conn = MySQLdb.Connect(host = '127.0.0.1',

port = 3306,user = 'root',passwd = 'XXXXX',db = 'XXXX',charset = 'utf8')

cursor = conn.cursor()

sql_insert = \"insert into resume(ID, name,sex,age,experience,education,pay,ad,job,job_experience,education_experience)values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)\"try:

cursor.execute(sql_insert, (ID, name,sex,age,experience,education,pay,ad,job,job_experience,education_experience))conn.commit()

except Exception as e:print e

conn.rollback()finally:

cursor.close()conn.close()12345678910111213141516171819202122232425262728293031323334353637383940414243444546

474849

利⽤python爬取58同城简历数据第⼀获取整个页⾯

第⼆筛选你想要的数据第三把数据保存到数据库连接数据库创建插⼊语句插⼊数据关闭数据库执⾏程序结果附录代码

⽂章标签: python 58同城 数据个⼈分类: 爬⾍

相关热词: 445利⽤ cpu利⽤ beef利⽤ fck利⽤ 利⽤csrf▼查看关于本篇⽂章更多信息

不会这些技术,⼤数据开发薪资不会⾼?

⼤数据技术与运⽤的成熟,应⽤集中于互联⽹、⾦融、医疗、新能源、通信和房地产等⾏业。整理平均薪资情况和⼤数据学习⼤纲供查看

想对作者说点什么? 我来说⼀句weixin_42498033

weixin_424980332018-07-09 10:31:57#7楼

请问博主能给⼀下所⽤到的代码吗?谢谢!1782764024@qq.comqq_23704631

qq_237046312018-04-13 21:42:41#6楼我联系⽅式qq1018141445qq_23704631

qq_237046312018-04-13 21:41:38#5楼能留个联系⽅式不

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sceh.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务