抽屉网
==========================(一)=========================
# -*- coding: utf-8 -*-
import scrapy
class ChoutiSpider(scrapy.Spider):
? ? name = 'chouti'
? ? allowed_domains = ['chouti.com']
? ? start_urls = ['http://dig.chouti.com/']
? ? # def start_requests(self):
? ? #? ? ?print('//')
? ? # 第一次請求之后返回的響應
? ? # 有的網站在返回登錄頁面時,會攜帶一些登錄需要的參數,例如csrf_token,xsrf等等,需要先從登錄頁面中提取所需參數,再發送POST請求
? ? def parse(self, response):
? ? ? ? '''
? ? ? ? 通過發送POST請求,模擬登錄
? ? ? ? response 可以從response中提取一些登錄需要參數
? ? ? ? '''
? ? ? ? # 重新發起一次POST登錄請求
? ? ? ? # FormRequest() 是scrapy提供的用于發送POST請求的類
? ? ? ? yield scrapy.FormRequest(
? ? ? ? ? ? # 登錄地址
? ? ? ? ? ? url='http://dig.chouti.com/login',
? ? ? ? ? ? # 請求參數
? ? ? ? ? ? formdata={
? ? ? ? ? ? ? ? 'phone':'8615237034401',
? ? ? ? ? ? ? ? 'password':'13243259989',
? ? ? ? ? ? ? ? 'oneMonth':'1'
? ? ? ? ? ? },
? ? ? ? ? ? # 回調函數
? ? ? ? ? ? callback=self.parse_index
? ? ? ? )
? ? def parse_index(self, response):
? ? ? ? '''
? ? ? ? {"result":{"code":"9999", "message":"", "data":{"complateReg":"0","destJid":"cdu_52091364220"}}}
? ? ? ? '''
? ? ? ? print(response.text)
? ? ? ? print('...')
? ? ? ? yield scrapy.Request(
? ? ? ? ? ? url='http://dig.chouti.com/user/link/saved/1',
? ? ? ? ? ? callback=self.parse_index
? ? ? ? )
=======================(二)==================================
# -*- coding: utf-8 -*-
import scrapy
import codecs
import requests
class Chouti2Spider(scrapy.Spider):
? ? name = 'chouti2'
? ? allowed_domains = ['chouti.com']
? ? # start_urls 可以不用設置
? ? # start_urls = ['http://dig.chouti.com/']
? ? # 整個爬蟲程序第一個調用的函數,第一個請求就是從這發出去的,
? ? def start_requests(self):
? ? ? ? # for循環遍歷start_urls,根據取出url創建request對象,yield request對象
? ? ? ? # 發送登錄的POST請求
? ? ? ? yield scrapy.FormRequest(
? ? ? ? ? ? url='http://dig.chouti.com/login',
? ? ? ? ? ? formdata={
? ? ? ? ? ? ? ? 'phone':'8615237034401',
? ? ? ? ? ? ? ? 'password':'13243259989',
? ? ? ? ? ? ? ? 'oneMonth':'1'
? ? ? ? ? ? },
? ? ? ? ? ? # callback如果不指定,回調parse()函數
? ? ? ? ? ? # callback=self.parse
? ? ? ? ? ? headers={
? ? ? ? ? ? ? ? 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0'
? ? ? ? ? ? }
? ? ? ? )
? ? def parse(self, response):
? ? ? ? # 取出cookies
? ? ? ? headers = response.headers.iteritems()
? ? ? ? for x in headers:
? ? ? ? ? ? if b'Set-Cookie' in x:
? ? ? ? ? ? ? ? cookies = x[1]
? ? ? ? ? ? ? ? cookies = [ck.decode('utf-8').split(';')[0] for ck in cookies]
? ? ? ? ? ? ? ? cookie_str = ';'.join(cookies[:2])
? ? ? ? # 直接訪問登錄后才能訪問頁面
? ? ? ? yield scrapy.Request(
? ? ? ? ? ? url='http://dig.chouti.com/user/link/saved/1',
? ? ? ? ? ? callback=self.parse_center,
? ? ? ? ? ? headers={
? ? ? ? ? ? ? ? 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0',
? ? ? ? ? ? ? ? 'Host':'dig.chouti.com',
? ? ? ? ? ? ? ? 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
? ? ? ? ? ? ? ? 'Referer':'http://dig.chouti.com/',
? ? ? ? ? ? ? ? 'Connection':'keep-alive',
? ? ? ? ? ? ? ? 'Cookie':cookie_str,# gpsd 這個cookie有問題
? ? ? ? ? ? ? ? 'Upgrade-Insecure-Requests':'1'
? ? ? ? ? ? }
? ? ? ? )
? ? def parse_center(self, response):
? ? ? ? print(response.text)
? ? ? ? with codecs.open('1.html','w+',encoding='utf-8') as f:
? ? ? ? ? ? f.write(response.text)
'd217b0e9ece60ada35f57015f54e8f59'
'd217b0e9ece60ada35f57015f54e8f59'
'd217b0e9ece60ada35f57015f54e8f59'
總結
- 上一篇: 发型识别大总结
- 下一篇: 卷积神经网络调参技巧(2)--过拟合(D