python2.7實(shí)現(xiàn)爬蟲網(wǎng)頁數(shù)據(jù)
最近剛學(xué)習(xí)Python,做了個(gè)簡單的爬蟲,作為一個(gè)簡單的demo希望幫助和我一樣的初學(xué)者。
代碼使用python2.7做的爬蟲 抓取51job上面的職位名,公司名,薪資,發(fā)布時(shí)間等等。
直接上代碼,代碼中注釋還算比較清楚 ,沒有安裝mysql需要屏蔽掉相關(guān)代碼:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import urllib
import urllib2
import codecs
import re
import time
import logging
import MySQLdb
class Jobs(object):
# 初始化
"""docstring for Jobs"""
def __init__(self):
super(Jobs, self).__init__()
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
#數(shù)據(jù)庫的操作,沒有mysql可以做屏蔽
self.db = MySQLdb.connect('127.0.0.1','root','rootroot','MySQL_Test',charset='utf8')
self.cursor = self.db.cursor()
#log日志的顯示
self.logger = logging.getLogger("sjk")
self.logger.setLevel(level=logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.FileHandler('log.txt')
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
self.logger.addHandler(handler)
self.logger.info('初始化完成')
# 模擬請求數(shù)據(jù)
def jobshtml(self, key, page='1'):
try:
self.logger.info('開始請求第' + page + '頁')
#網(wǎng)頁url
searchurl = "https://search.51job.com/list/040000,000000,0000,00,9,99,{key},2,{page}.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:59.0) Gecko/20100101 Firefox/59.0'
#設(shè)置請求頭
header = {'User-Agent': user_agent, 'Host': 'search.51job.com',
'Referer': 'https://www.51job.com/'}
#拼接url
finalUrl = searchurl.format(key=key, page=page)
request = urllib2.Request(finalUrl, headers=header)
response = urllib2.urlopen(request)
#等待網(wǎng)頁加載完成
time.sleep(3)
#gbk格式解碼
info = response.read().decode('gbk')
self.logger.info('請求網(wǎng)頁網(wǎng)頁')
self.decodeHtml(info=info, key=key, page=page)
except urllib2.HTTPError as e:
print e.reason
# 解析網(wǎng)頁數(shù)據(jù)
def decodeHtml(self, info, key, page):
self.logger.info('開始解析網(wǎng)頁數(shù)據(jù)')
#BeautifulSoup 解析網(wǎng)頁
soup = BeautifulSoup(info, 'html.parser')
#找到class = t1 t2 t3 t4 t5 的標(biāo)簽數(shù)據(jù)
ps = soup.find_all(attrs={"class": re.compile(r'^t[1-5].*')})
#打開txt文件 a+ 代表追加
f = codecs.open(key + '.txt', 'a+', 'UTF-8')
#清除之前的數(shù)據(jù)信息
f.truncate()
f.write('\n------------' + page + '--------------\n')
count = 1
arr = []
#做一些字符串的處理,形成數(shù)據(jù)格式 iOS開發(fā)工程師 有限公司 深圳-南山區(qū) 0.9-1.6萬/月 05-16
for pi in ps:
spe = " "
finalstr = pi.getText().strip()
arr.append(finalstr)
if count % 5 == 0:
#每一條數(shù)據(jù)插入數(shù)據(jù)庫,如果沒有安裝mysql 可以將當(dāng)前行注釋掉
self.connectMySQL(arr=arr)
arr = []
spe = "\n"
writestr = finalstr + spe
count += 1
f.write(writestr)
f.close()
self.logger.info('解析完成')
#數(shù)據(jù)庫操作 沒有安裝mysql 可以屏蔽掉
def connectMySQL(self,arr):
work=arr[0]
company=arr[1]
place=arr[2]
salary=arr[3]
time=arr[4]
query = "select * from Jobs_tab where \
company_name='%s' and work_name='%s' and work_place='%s' \
and salary='%s' and time='%s'" %(company,work,place,salary,time)
self.cursor.execute(query)
queryresult = self.cursor.fetchall()
#數(shù)據(jù)庫中不存在就插入數(shù)據(jù) 存在就可以更新數(shù)據(jù) 不過我這邊沒有寫
if len(queryresult) > 0:
sql = "insert into Jobs_tab(work_name,company_name,work_place,salary\
,time) values('%s','%s','%s','%s','%s')" %(work,company,place,salary,time)
try:
self.cursor.execute(sql)
self.db.commit()
except Exception as e:
self.logger.info('寫入數(shù)據(jù)庫失敗')
#模擬登陸
# def login(self):
# data = {'action':'save','isread':'on','loginname':'18086514327','password':'kui4131sjk'}
# 開始抓取 主函數(shù)
def run(self, key):
# 只要前5頁的數(shù)據(jù) key代表搜索工做類型 這邊我是用的ios page是頁數(shù)
for x in xrange(1, 6):
self.jobshtml(key=key, page=str(x))
self.logger.info('寫入數(shù)據(jù)庫完成')
self.db.close()
if __name__ == '__main__':
Jobs().run(key='iOS')
這樣抓取網(wǎng)頁數(shù)據(jù)格式如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python實(shí)現(xiàn)數(shù)據(jù)可視化看如何監(jiān)控你的爬蟲狀態(tài)【推薦】
- Python3實(shí)現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
- python爬蟲爬取網(wǎng)頁表格數(shù)據(jù)
- python中數(shù)據(jù)爬蟲requests庫使用方法詳解
- Python爬蟲實(shí)例_城市公交網(wǎng)絡(luò)站點(diǎn)數(shù)據(jù)的爬取方法
- Python爬蟲_城市公交、地鐵站點(diǎn)和線路數(shù)據(jù)采集實(shí)例
- python爬蟲今日熱榜數(shù)據(jù)到txt文件的源碼
相關(guān)文章
Python+Qt身體特征識別人數(shù)統(tǒng)計(jì)源碼窗體程序(使用步驟)
這篇文章主要介紹了Python+Qt身體特征識別人數(shù)統(tǒng)計(jì)源碼窗體程序(使用步驟),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
Python連接Mysql實(shí)現(xiàn)圖書借閱系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python連接Mysql實(shí)現(xiàn)圖書借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Python網(wǎng)絡(luò)爬蟲之Web網(wǎng)頁基礎(chǔ)
我們在學(xué)習(xí)爬蟲之前,要先了解網(wǎng)頁的組成,只有我們了解其組成嗎,才可以方能百戰(zhàn)百勝,文章中有詳細(xì)的代碼示例,需要的朋友可以參考一下2023-04-04
Python一行代碼識別車牌號碼實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Python一行代碼識別車牌號碼實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

