利用python爬取58同城簡曆資料

來源:互聯網
上載者:User

標籤:nta   運算式   finally   fan   返回   新手   mysqldb   描述   list   

利用python爬取58同城簡曆資料
利用python爬取58同城簡曆資料
最近接到一個工作,需要擷取58同城上面的簡曆資訊(http://gz.58.com/qzyewu/)。最開始想到是用python裡面的scrapy架構製作爬蟲。但是在製作的時候,發現內容不能被儲存在本地變數 response 中。當我通過shell載入網頁後,雖然內容能被儲存在response中,用xpath對我需要的資料進行擷取時,返回的都是空值。考慮到資料都在源碼中,於是我使用python裡的beautifulSoup通過下載源碼的方式去擷取資料,然後插入到資料庫。

需要的python包urllib2?,beautifulSoup,MySQLdb,re

第一,擷取整個頁面

coding:utf-8

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上的資料

第二,篩選你想要的資料
這裡需要用到Regex,python提供了強大的Regex,不清楚的小夥伴可以參考一下資料(http://www.runoob.com/regexp/regexp-syntax.html)

比如,我們需要擷取姓名

通過控制台可以看到名字所在的位置
這裡寫圖片描述

可用Regex進行匹配,代碼如下:

name = re.findall(r‘(?<=class="name">).*?(?=)‘,str(soup))
1
運行程式,發現返回結果為空白。

檢查Regex是無誤的,我們觀察之前返回的soup,發現他返回的源碼與網頁上的源碼是不一樣的。所有我們根據觀察網頁上的源碼寫的Regex不能再返回的源碼中匹配到相應的內容。因此我們只能通過觀察返回的源碼寫Regex。

這裡寫圖片描述

在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使用簡單,適合新手練手。

附錄:代碼

coding:utf-8

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‘(?<=

)\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()
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樓
請問博主能給一下所用到的代碼嗎?謝謝![email protected]
qq_23704631
qq_237046312018-04-13 21:42:41#6樓
我連絡方式qq1018141445
qq_23704631
qq_237046312018-04-13 21:41:38#5樓
能留個連絡方式不

利用python爬取58同城簡曆資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.