利用python爬取58同城简历数据
利用python爬取58同城简历数据
最近接到一个工做,须要获取58同城上面的简历信息(http://gz.58.com/qzyewu/)。最开始想到是用python里面的scrapy框架制做爬虫。可是在制做的时候,发现内容不能被存储在本地变量 response 中。当我经过shell载入网页后,虽然内容能被储存在response中,用xpath对我须要的数据进行获取时,返回的都是空值。考虑到数据都在源码中,因而我使用python里的beautifulSoup经过下载源码的方式去获取数据,而后插入到数据库。javascript
须要的python包urllib2,beautifulSoup,MySQLdb,rehtml
第一,获取整个页面java
import urllib2
from BeautifulSoup import BeautifulSoup
url='http://jianli.58.com/resume/91655325401100'
content = urllib2.urlopen(url).read()
soup=BeautifulSoup(content)
print soup
1
2
3
4
5
6
7
url为须要下载的网页
经过urllib2.urlopen()方法打开一个网页
read()方法读取url上的数据python
第二,筛选你想要的数据
这里须要用到正则表达式,python提供了强大的正则表达式,不清楚的小伙伴能够参考一下资料(http://www.runoob.com/regexp/regexp-syntax.html)mysql
好比,咱们须要获取姓名正则表达式
经过控制台能够看到名字所在的位置
这里写图片描述sql
可用正则表达式进行匹配,代码以下:shell
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()
1
2
3
4
5
6
7
由于要存储中文,因此在这里设置编码格式为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)"
1
2
3
4
插入数据
cursor.execute(sql_insert,(ID,name,sex,age,experience,education
,pay,ad,job,job_experience,education_experience))
conn.commit()
1
2
3
关闭数据库
cursor.close()
conn.close()
1
2
执行程序
报错了…
(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' 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使用简单,适合新手练手。
附录:代码
import urllib2
from BeautifulSoup import BeautifulSoup
import 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'(?<=
).\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()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
利用python爬取58同城简历数据
第一获取整个页面
第二筛选你想要的数据
第三把数据保存到数据库
链接数据库
建立插入语句
插入数据
关闭数据库
执行程序
结果
附录代码
文章标签: python 58同城 数据
我的分类: 爬虫
相关热词: 445利用 cpu利用 beef利用 fck利用 利用csrf
▼查看关于本篇文章更多信息
不会这些技术,大数据开发薪资不会高?
大数据技术与运用的成熟,应用集中于互联网、金融、医疗、新能源、通讯和房地产等行业。整理平均薪资状况和大数据学习大纲供查看
想对做者说点什么? 我来讲一句 weixin_42498033 weixin_424980332018-07-09 10:31:57#7楼 请问博主能给一下所用到的代码吗?谢谢!1782764024@qq.com qq_23704631 qq_237046312018-04-13 21:42:41#6楼 我联系方式qq1018141445 qq_23704631 qq_237046312018-04-13 21:41:38#5楼 能留个联系方式不