【百度云搜索,搜各種資料:http://www.lqkweb.com】
【搜網盤,搜各種資料:http://www.swpan.cn】
Requests請求
Requests請求就是我們在爬蟲文件寫的 Requests()方法,也就是提交一個請求地址, Requests請求是我們自定義的 * *
Requests()方法提交一個請求
參數:
url= ?字符串類型url地址
callback= 回調函數名稱
method= 字符串類型請求方式,如果GET,POST
headers= 字典類型的,瀏覽器用戶代理
cookies= 設置cookies
meta= 字典類型鍵值對,向回調函數直接傳一個指定值
encoding= 設置網頁編碼
priority= 默認為0,如果設置的越高,越優先調度
dont_filter= 默認為False,如果設置為真,會過濾掉當前url
#?-*-?coding:?utf-8?-*-
import?scrapy
from?scrapy.http?import?Request,FormRequest
import?re
class?PachSpider(scrapy.Spider):????????????????????????????#定義爬蟲類,必須繼承scrapy.Spider
????name?=?'pach'???????????????????????????????????????????#設置爬蟲名稱
????allowed_domains?=?['www.luyin.org/']????????????????????#爬取域名
????#?start_urls?=?['']?????????????????????????????????????#爬取網址,只適于不需要登錄的請求,因為沒法設置cookie等信息
????header?=?{'User-Agent':'Mozilla/5.0?(Windows?NT?10.0;?WOW64;?rv:54.0)?Gecko/20100101?Firefox/54.0'}??#設置瀏覽器用戶代理
????def?start_requests(self):????#起始url函數,會替換start_urls
????????"""第一次請求一下登錄頁面,設置開啟cookie使其得到cookie,設置回調函數"""
????????return?[Request(
????????????url='http://www.luyin.org/',
????????????headers=self.header,
????????????meta={'cookiejar':1},???????#開啟Cookies記錄,將Cookies傳給回調函數
????????????callback=self.parse
????????)]
????def?parse(self,?response):
????????title?=?response.xpath('/html/head/title/text()').extract()
????????print(title)
?
Response響應
Response響應是由downloader返回的響應
Response響應參數
headers 返回響應頭
status 返回狀態嗎
body 返回頁面內容,字節類型
url 返回抓取url
#?-*-?coding:?utf-8?-*-
import?scrapy
from?scrapy.http?import?Request,FormRequest
import?re
class?PachSpider(scrapy.Spider):????????????????????????????#定義爬蟲類,必須繼承scrapy.Spider
????name?=?'pach'???????????????????????????????????????????#設置爬蟲名稱
????allowed_domains?=?['www.luyin.org/']????????????????????#爬取域名
????#?start_urls?=?['']?????????????????????????????????????#爬取網址,只適于不需要登錄的請求,因為沒法設置cookie等信息
????header?=?{'User-Agent':'Mozilla/5.0?(Windows?NT?10.0;?WOW64;?rv:54.0)?Gecko/20100101?Firefox/54.0'}??#設置瀏覽器用戶代理
????def?start_requests(self):????#起始url函數,會替換start_urls
????????"""第一次請求一下登錄頁面,設置開啟cookie使其得到cookie,設置回調函數"""
????????return?[Request(
????????????url='http://www.luyin.org/',
????????????headers=self.header,
????????????meta={'cookiejar':1},???????#開啟Cookies記錄,將Cookies傳給回調函數
????????????callback=self.parse
????????)]
????def?parse(self,?response):
????????title?=?response.xpath('/html/head/title/text()').extract()
????????print(title)
????????print(response.headers)
????????print(response.status)
????????#?print(response.body)
????????print(response.url)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
