Python使用scrapy爬取陽光熱線問政平臺過程解析
目的:爬取陽光熱線問政平臺問題反映每個帖子里面的標題、內(nèi)容、編號和帖子url
CrawlSpider版流程如下:
創(chuàng)建爬蟲項目dongguang
scrapy startproject dongguang
設(shè)置items.py文件
# -*- coding: utf-8 -*- import scrapy class NewdongguanItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() # pass # 每頁的帖子鏈接 url = scrapy.Field() # 帖子標題 title = scrapy.Field() # 帖子編號 number = scrapy.Field() # 帖子內(nèi)容 content = scrapy.Field()
在spiders目錄里面,創(chuàng)建并編寫爬蟲文件sun.py
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from dongguan.items import DongguanItem
class SunSpider(CrawlSpider):
name = 'dg'
allowed_domains = ['wz.sun0769.com']
start_urls = ['http://wz.sun0769.com/html/top/report.shtml']
# rules是Rule的集合,每個rule規(guī)則同時執(zhí)行。另外,如果發(fā)現(xiàn)web服務(wù)器有反爬蟲機制如返回一個假的url,則可以使用Rule里面的參數(shù)process_links調(diào)用一個自編函數(shù)來處理url后返回一個真的url
rules = (
# 每個url都有一個獨一無二的指紋,每個爬蟲項目都有一個去重隊列
# Rule里面沒有回調(diào)函數(shù),則默認對匹配的鏈接要跟進,就是對匹配的鏈接在進行請求獲取響應(yīng)后對響應(yīng)里面匹配的鏈接繼續(xù)跟進,只不過沒有回調(diào)函數(shù)對響應(yīng)數(shù)據(jù)進行處理
# Rule(LinkExtractor(allow="page="))如果設(shè)置為follow=False,則不會跟進,只顯示當前頁面匹配的鏈接。如設(shè)置為follow=True,則會對每個匹配的鏈接發(fā)送請求獲取響應(yīng)進而從每個響應(yīng)里面再次匹配跟進,直至沒有。python遞歸深度默認為不超過1000,否則會報異常
Rule(LinkExtractor(allow="page=")),
Rule(LinkExtractor(allow='http://wz.sun0769.com/html/question/\d+/\d+.shtml'),callback='parse_item')
)
def parse_item(self, response):
print(response.url)
item = DongguanItem()
item['url'] = response.url
item['title'] = response.xpath('//div[@class="pagecenter p3"]//strong/text()').extract()[0]
item['number'] = response.xpath('//div[@class="pagecenter p3"]//strong/text()').extract()[0].split(' ')[-1].split(':')[-1]
# 對帖子里面有圖片的處理,發(fā)現(xiàn)沒有圖片時則沒有class="contentext"的div標簽,以此作為標準獲取帖子內(nèi)容
if len(response.xpath('//div[@class="contentext"]')) == 0:
item['content'] = ''.join(response.xpath('//div[@class="c1 text14_2"]/text()').extract())
else:
item['content'] = ''.join(response.xpath('//div[@class="contentext"]/text()').extract())
yield item
編寫管道pipelines.py文件
# -*- coding: utf-8 -*-
import json
class DongguanPipeline(object):
def __init__(self):
self.file = open('dongguan.json','w')
def process_item(self, item, spider):
content = json.dumps(dict(item),ensure_ascii=False).encode('utf-8') + '\n'
self.file.write(content)
return item
def closespider(self):
self.file.close()
編寫settings.py文件
# -*- coding: utf-8 -*-
BOT_NAME = 'dongguan'
SPIDER_MODULES = ['dongguan.spiders']
NEWSPIDER_MODULE = 'dongguan.spiders'
# log日志文件默認保存在當前目錄,下面為日志級別,當大于或等于INFO時將被保存
LOG_FILE = 'dongguan.log'
LOG_LEVEL = 'INFO'
# 爬取深度設(shè)置
# DEPTH_LIMIT = 1
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'dongguan (+http://www.yourdomain.com)'
# Obey robots.txt rules
# ROBOTSTXT_OBEY = True
# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32
# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'dongguan.pipelines.DongguanPipeline': 300,
}
測試運行爬蟲,終端執(zhí)行命令(只要在項目目錄內(nèi)即可)
scrapy crawl dg
Spider版流程如下:
創(chuàng)建爬蟲項目newdongguang
scrapy startproject newdongguan
設(shè)置items.py文件
# -*- coding: utf-8 -*-
import scrapy
class NewdongguanItem(scrapy.Item):
# 每頁的帖子鏈接
url = scrapy.Field()
# 帖子標題
title = scrapy.Field()
# 帖子編號
number = scrapy.Field()
# 帖子內(nèi)容
content = scrapy.Field()
在spiders目錄里面,創(chuàng)建并編寫爬蟲文件newsun.py
# -*- coding: utf-8 -*-
import scrapy
from newdongguan.items import NewdongguanItem
class NewsunSpider(scrapy.Spider):
name = 'ndg'
# 設(shè)置爬取的域名范圍,可寫可不寫,不寫則表示爬取時候不限域名,結(jié)果有可能會導(dǎo)致爬蟲失控。
allowed_domains = ['wz.sun0769.com']
offset = 0
url = 'http://wz.sun0769.com/index.php/question/report?page=' + str(offset)
start_urls = [url]
def parse(self, response):
link_list = response.xpath("http://a[@class='news14']/@href").extract()
for each in link_list:
# 對每頁的帖子發(fā)送請求,獲取帖子內(nèi)容里面指定數(shù)據(jù)返回給管道文件
yield scrapy.Request(each,callback=self.deal_link)
self.offset += 30
if self.offset <= 124260:
url = 'http://wz.sun0769.com/index.php/question/report?page=' + str(self.offset)
# 對指定分頁發(fā)送請求,響應(yīng)交給parse函數(shù)處理
yield scrapy.Request(url,callback=self.parse)
# 從每個分頁帖子內(nèi)容獲取數(shù)據(jù),返回給管道
def deal_link(self,response):
item = NewdongguanItem()
item['url'] = response.url
item['title'] = response.xpath("http://div[@class='pagecenter p3']//strong[@class='tgray14']/text()").extract()[0]
item['number'] = response.xpath("http://div[@class='pagecenter p3']//strong[@class='tgray14']/text()").extract()[0].split(' ')[-1].split(':')[-1]
if len(response.xpath("http://div[@class='contentext']")) == 0:
item['content'] = ''.join(response.xpath("http://div[@class='c1 text14_2']/text()").extract())
else:
item['content'] = ''.join(response.xpath("http://div[@class='contentext']/text()").extract())
yield item
編寫管道pipelines.py文件
# -*- coding: utf-8 -*-
import codecs
import json
class NewdongguanPipeline(object):
def __init__(self):
# 使用codecs寫文件,直接設(shè)置文件內(nèi)容編碼格式,省去每次都要對內(nèi)容進行編碼
self.file = codecs.open('newdongguan.json','w',encoding = 'utf-8')
# 以前文件寫法
# self.file = open('newdongguan.json','w')
def process_item(self, item, spider):
print(item['title'])
content = json.dumps(dict(item),ensure_ascii=False) + '\n'
# 以前文件寫法
# self.file.write(content.encode('utf-8'))
self.file.write(content)
return item
def close_spider(self):
self.file.close()
編寫settings.py文件
# -*- coding: utf-8 -*-
BOT_NAME = 'newdongguan'
SPIDER_MODULES = ['newdongguan.spiders']
NEWSPIDER_MODULE = 'newdongguan.spiders'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'newdongguan (+http://www.yourdomain.com)'
USER_AGENT = 'User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;'
# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'newdongguan.pipelines.NewdongguanPipeline': 300,
}
測試運行爬蟲,終端執(zhí)行命
srapy crawl ndg
備注:markdown語法關(guān)于代碼塊縮進問題,可通過tab鍵來解決。而簡單文本則可以通過回車鍵來解決,如Spider版流程如下:和1. 創(chuàng)建爬蟲項目newdongguang
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Python Scrapy框架第一個入門程序示例
- python3 Scrapy爬蟲框架ip代理配置的方法
- Python利用Scrapy框架爬取豆瓣電影示例
- python scrapy重復(fù)執(zhí)行實現(xiàn)代碼詳解
- Python scrapy增量爬取實例及實現(xiàn)過程解析
- VirtualBox CentOS7.7.1908 Python3.8 搭建Scrapy開發(fā)環(huán)境【圖文教程】
- python網(wǎng)絡(luò)爬蟲 Scrapy中selenium用法詳解
- python scrapy爬蟲代碼及填坑
- 基于python框架Scrapy爬取自己的博客內(nèi)容過程詳解
- Python爬蟲 scrapy框架爬取某招聘網(wǎng)存入mongodb解析
- Python3環(huán)境安裝Scrapy爬蟲框架過程及常見錯誤
- 圖文詳解python安裝Scrapy框架步驟
- 一步步教你用python的scrapy編寫一個爬蟲
- 詳解python3 + Scrapy爬蟲學習之創(chuàng)建項目
- 詳解Python網(wǎng)絡(luò)框架Django和Scrapy安裝指南
- Scrapy框架爬取Boss直聘網(wǎng)Python職位信息的源碼
- windows下搭建python scrapy爬蟲框架步驟
- python爬蟲庫scrapy簡單使用實例詳解
相關(guān)文章
python elasticsearch從創(chuàng)建索引到寫入數(shù)據(jù)的全過程
這篇文章主要介紹了python elasticsearch從創(chuàng)建索引到寫入數(shù)據(jù)的方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
opencv+python識別七段數(shù)碼顯示器的數(shù)字(數(shù)字識別)
本文主要介紹了opencv+python識別七段數(shù)碼顯示器的數(shù)字(數(shù)字識別),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
windows端python版本管理工具pyenv-win安裝使用
這篇文章主要介紹了如何通過git方式下載和配置pyenv-win,包括下載、克隆倉庫、配置環(huán)境變量等步驟,同時還詳細介紹了如何使用pyenv-win管理Python版本,需要的朋友可以參考下2025-01-01
手把手教你用Python中的Linting提高代碼質(zhì)量
Python是一種不斷發(fā)展的語言,隨著它的演化和擴展,可用工具和開發(fā)策略的數(shù)量也在增加,近來流行的一個過程是linting—檢查代碼的潛在問題,下面這篇文章主要給大家介紹了關(guān)于用Python中Linting提高代碼質(zhì)量的相關(guān)資料,需要的朋友可以參考下2023-01-01
網(wǎng)站滲透常用Python小腳本查詢同ip網(wǎng)站
這篇文章主要介紹了網(wǎng)站滲透常用Python小腳本查詢同ip網(wǎng)站,需要的朋友可以參考下2017-05-05
Python 實現(xiàn)國產(chǎn)SM3加密算法的示例代碼
這篇文章主要介紹了Python 實現(xiàn)國產(chǎn)SM3加密算法的示例代碼,幫助大家更好的理解和學習密碼學,感興趣的朋友可以了解下2020-09-09

