小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Scrapy爬蟲實(shí)踐之搜索并獲取前程無(wú)憂職位信息(基礎(chǔ)篇)

 Levy_X 2017-09-05

一、開發(fā)環(huán)境

       OS:Windows 7 64bit旗艦版       
       Python:2.7.10       
       Scrapy:1.0.3       
       MySQL:5.6.21       
       Sublime Text2:2.0.2       
  

二、目標(biāo)

       通過(guò)在前程無(wú)憂的職位搜索中輸入職位關(guān)鍵詞獲取相應(yīng)的職位信息,現(xiàn)在我們通過(guò)Scrapy爬蟲來(lái)實(shí)現(xiàn)這個(gè)功能,自動(dòng)幫我們獲取相關(guān)的職位信息,并保存成.json格式和保存到MySQL數(shù)據(jù)庫(kù)。

三、實(shí)現(xiàn)步驟

      Scrapy是一個(gè)比較流行的Python爬蟲框架,Scrapy爬蟲的基本實(shí)現(xiàn)流程如下:

       1.通過(guò)scrapy startproject spiderproject 來(lái)創(chuàng)建一個(gè)新的爬蟲工程,spiderproject是我們自己命名的爬蟲工程。比如本例子,我們創(chuàng)建一個(gè)工程scrapy startproject qcwy,qcwy即為我們的工程名。

       2.定義我們要解析具體數(shù)據(jù)的Item結(jié)構(gòu),在items.py文件中。

       3.在pipelines.py中實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)的功能,可以在這里實(shí)現(xiàn)我們抓取的數(shù)據(jù)保存在.json文件中,或者M(jìn)ySQL中,或者SQLite中,或者M(jìn)ongoDB中,或者其他你要保存的格式或者數(shù)據(jù)庫(kù)中。

       4.在第1步創(chuàng)建的工程文件夾下的spider文件目錄下創(chuàng)建一個(gè)新的后綴為.py的文件命名最好包含spider這個(gè)單詞,因?yàn)槲覀円谶@個(gè)文件中實(shí)現(xiàn)核心功能Spider。

       5.在setting.py中進(jìn)行一些相關(guān)的設(shè)置。


     下面我們以詳細(xì)實(shí)例說(shuō)明如何以Scrapy實(shí)現(xiàn)我們的目的:

      1.在CMD中輸入 cd c:\  進(jìn)入c盤根目錄

       2.輸入scrapy startproject qcwy,我們可以看到在c盤根目錄下看到qcwy這個(gè)文件夾(本文假如你的開發(fā)環(huán)境配置沒(méi)有任何問(wèn)題,具體配置請(qǐng)參照Scrapy文檔)。

       進(jìn)入qcwy文件夾后,可以看到還有一個(gè)qcwy的文件夾和一個(gè)scrapy.cfg項(xiàng)目配置文件,不用管它,繼續(xù)進(jìn)入qcwy這個(gè)文件夾,可以看到:


        3.定義我們要解析的數(shù)據(jù)的Item類

       實(shí)現(xiàn)items.py詳細(xì)代碼:

from scrapy.item import Item, Field class QcwyItem(Item): #定義要抓取信息的Item結(jié)構(gòu) title = Field() #職位名稱 link = Field() #詳情鏈接 company = Field() #公司名稱 updatetime = Field() #更新時(shí)間

  4.實(shí)現(xiàn)pipelines.py的詳細(xì)代碼:
import json import codecs import MySQLdb import MySQLdb.cursors from twisted.enterprise import adbapi from scrapy import signals class QcwyJsonPipeline(object): def __init__(self): self.file = codecs.open('..\\qcwy\\qcwy\\qcwy.json', 'w', encoding = 'utf-8') def process_item(self, item, spider): line = json.dumps(dict(item), ensure_ascii = False) '\n' self.file.write(line) return item def spider_closed(self, spider): self.file.close() class QcwyMySQLPipeline(object): '''docstring for MySQLPipeline''' def __init__(self): self.connpool = adbapi.ConnectionPool('MySQLdb', host = '127.0.0.1', db = 'qcwy', user = 'root', passwd = '', cursorclass = MySQLdb.cursors.DictCursor, charset = 'utf8', use_unicode = True ) def process_item(self, item, spider): query = self.connpool.runInteraction(self._conditional_insert, item) query.addErrback(self.handle_error) return item def _conditional_insert(self, tx, item): if item.get('title'): tx.execute('insert into detail (title, link, company, updatetime) values(%s, %s, %s, %s)', (item['title'], item['link'], item['company'], item['updatetime'])) def handle_error(self, e): log.err(e)
  

       5.Spider的實(shí)現(xiàn) 

# -*- coding: utf-8 -*- import logging import scrapy import urllib import codecs from scrapy.selector import Selector from qcwy.items import QcwyItem import sys reload(sys) sys.setdefaultencoding('utf-8') keyword = 'Python' #把字符串編碼成符合url規(guī)范的編碼 keywordcode = urllib.quote(keyword) is_start_page = True class TestfollowSpider(scrapy.Spider): name = 'qcwysearch' allowed_domains = [''] start_urls = [ 'http://search./jobsearch/search_result.php?fromJs=1&jobarea=030200%2C00&funtype=0000&industrytype=00&keyword=' keywordcode, ] def parse(self, response): global is_start_page url = '' #從開始頁(yè)面開始解析數(shù)據(jù),開始頁(yè)面start_urls if is_start_page: url = self.start_urls[0] is_start_page = False else: href = response.xpath('//table[@class='searchPageNav']/tr/td[last()]/a/@href') url = response.urljoin(href.extract()) yield scrapy.Request(url, callback=self.parse_dir_contents) def parse_dir_contents(self, response): for sel in response.xpath('//table[@id='resultList']/tr[@class='tr0']'): item = QcwyItem() temp = sel.xpath('td[@class='td1']/a/text()').extract() if len(temp) > 0: item['title'] = temp[0] keyword temp[-1] else: item['title'] = keyword item['link'] = sel.xpath('td[@class='td1']/a/@href').extract()[0] item['company'] = sel.xpath('td[@class='td2']/a/text()').extract()[0] item['updatetime'] = sel.xpath('td[@class='td4']/span/text()').extract()[0] yield item next_page = response.xpath('//table[@class='searchPageNav']/tr/td[last()]/a/@href') if next_page: url = response.urljoin(next_page[0].extract()) yield scrapy.Request(url, self.parse_dir_contents)

     6.在setting.py中加入:

ITEM_PIPELINES = { 'qcwy.pipelines.QcwyJsonPipeline': 300, 'qcwy.pipelines.QcwyMySQLPipeline': 800, }

    
   7.在CMD中輸入cd c:\qcwy進(jìn)入工程目錄

       輸入scrapy crawl qcwysearch

       然后查看生成的json文件,可以看到以下我們想要的信息:

{'company': '廣東威法科技發(fā)展有限公司', 'link': 'http://search./job/70631365,c.html', 'updatetime': '2015-09-29', 'title': '中級(jí)后端Python工程師'} {'company': '珠海橫琴新區(qū)蓋網(wǎng)通傳媒有限公司廣州分公司', 'link': 'http://search./job/69706486,c.html', 'updatetime': '2015-09-29', 'title': '開發(fā)工程師Python開發(fā)工程師'} {'company': '廣州明朝互動(dòng)科技股份有限公司', 'link': 'http://search./job/69898292,c.html', 'updatetime': '2015-09-29', 'title': '開發(fā)工程師Python開發(fā)工程師'} {'company': '廣州市賽酷比軟件有限公司', 'link': 'http://search./job/69816266,c.html', 'updatetime': '2015-09-29', 'title': '分布式軟件架構(gòu)師(Python...'} {'company': '廣州市賽酷比軟件有限公司', 'link': 'http://search./job/69816017,c.html', 'updatetime': '2015-09-29', 'title': '爬蟲工程師Python爬蟲工程師'} {'company': '廣州杰升信息科技有限公司', 'link': 'http://search./job/51077705,c.html', 'updatetime': '2015-09-29', 'title': '工程師Python工程師'} {'company': '廣州飛屋網(wǎng)絡(luò)科技有限公司', 'link': 'http://search./job/71800002,c.html', 'updatetime': '2015-09-29', 'title': '開發(fā)工程師Python開發(fā)工程師'} {'company': '廣州飛屋網(wǎng)絡(luò)科技有限公司', 'link': 'http://search./job/59931502,c.html', 'updatetime': '2015-09-29', 'title': '開發(fā)工程師Python開發(fā)工程師'} {'company': '廣州達(dá)新信息科技有限公司', 'link': 'http://search./job/69614188,c.html', 'updatetime': '2015-09-29', 'title': '開發(fā)工程師Python開發(fā)工程師'} {'company': '廣州埃立方通信技術(shù)有限公司', 'link': 'http://search./job/71920362,c.html', 'updatetime': '2015-09-29', 'title': '軟件工程師Python軟件工程師'} {'company': '廣州黑珍珠計(jì)算機(jī)技術(shù)有限公司', 'link': 'http://search./job/60592771,c.html', 'updatetime': '2015-09-29', 'title': '服務(wù)器端程序員Python服務(wù)器端程序員'} {'company': '聚力精彩(北京)信息技術(shù)有限公司', 'link': 'http://search./job/72246020,c.html', 'updatetime': '2015-09-29', 'title': '高級(jí)開發(fā)工程師AEBPython高級(jí)開發(fā)工程師AEB'} {'company': '廣州七樂(lè)康藥業(yè)連鎖有限公司', 'link': 'http://search./job/71362309,c.html', 'updatetime': '2015-09-29', 'title': '中級(jí)開發(fā)工程師Python中級(jí)開發(fā)工程師'} {'company': '廣州七樂(lè)康藥業(yè)連鎖有限公司', 'link': 'http://search./job/69404827,c.html', 'updatetime': '2015-09-29', 'title': ' 初級(jí)開發(fā)工程師Python 初級(jí)開發(fā)工程師'} {'company': '廣州七樂(lè)康藥業(yè)連鎖有限公司', 'link': 'http://search./job/69207304,c.html', 'updatetime': '2015-09-29', 'title': '資深開發(fā)工程師Python資深開發(fā)工程師'} {'company': '貴州格安科技有限公司廣州分公司', 'link': 'http://search./job/63157264,c.html', 'updatetime': '2015-09-29', 'title': '高級(jí)Python開發(fā)工程師'} {'company': '貴州格安科技有限公司廣州分公司', 'link': 'http://search./job/60035771,c.html', 'updatetime': '2015-09-29', 'title': ' 開發(fā)工程師Python 開發(fā)工程師'} {'company': '珠海麒潤(rùn)網(wǎng)絡(luò)科技有限公司', 'link': 'http://search./job/51153619,c.html', 'updatetime': '2015-09-29', 'title': 'PHP/Python開發(fā)工程師'} {'company': '廣州百倫貿(mào)易有限公司', 'link': 'http://search./job/71097137,c.html', 'updatetime': '2015-09-29', 'title': '開發(fā)工程師Python開發(fā)工程師'} {'company': '廣州暴雨網(wǎng)絡(luò)技術(shù)有限公司', 'link': 'http://search./job/69546409,c.html', 'updatetime': '2015-09-29', 'title': '服務(wù)端開發(fā)工程師Python服務(wù)端開發(fā)工程師'}

通過(guò)phpMyAdmin來(lái)查看MySQL數(shù)據(jù)庫(kù),可以看到:





      四、后記        這篇到這里就結(jié)束了,網(wǎng)絡(luò)爬蟲這個(gè)技術(shù)作為一個(gè)非常常規(guī)的獲取信息的手段,當(dāng)然如果用作生產(chǎn)工具的話,還有一些不足的地方。比如:如何使爬蟲的行為更像我們?cè)谑褂脼g覽器在瀏覽信息。接下來(lái),還會(huì)有本篇的后續(xù)進(jìn)階篇。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多