d:html
進入D盤git
scrapy startproject GitHubgithub
建立項目cookie
scrapy genspider github github.comsession
建立爬蟲app
編輯github.py:dom
# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request, FormRequest
class GithubSpider(scrapy.Spider):
name = 'github'
allowed_domains = ['github.com']
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Connection': 'keep-alive',
'Referer': 'https://github.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0',
'Content-Type': 'application/x-www-form-urlencoded'
}
# 請求頭
def start_requests(self):
# 重寫start_requests方法
urls = ['https://github.com/login']
for url in urls:
yield Request(url, meta={'cookiejar': 1}, callback=self.github_login)
# 經過meta傳入cookiejar特殊key,爬取url做爲參數傳給回調函數
# meta:字典格式的元數據
# cookiejar:是meta的一個特殊的key,經過cookiejar參數能夠支持多個會話對某網站進行爬取
# 能夠對cookie作標記1, 2, 3, 4......這樣scrapy就維持了多個會話
def github_login(self, response):
authenticity_token = response.xpath(".//*[@id='login']/form/input[2]/@value").extract_first()
# 首先從源碼中獲取到authenticity_token的值
return FormRequest.from_response(
response,
url='https://github.com/session',
meta={'cookiejar': response.meta['cookiejar']},
headers=self.headers,
formdata={
'authenticity_token': authenticity_token,
'commit': 'Sign in',
'login': '123456789@qq.com',
'password': 'abcdef@123456',
'utf8': '✓'
},
callback=self.github_after,
dont_click=True
# dont_click若是是True,表單數據將被提交,而不須要單擊任何元素
)
def github_after(self, response):
home_page = response.xpath(".//*[@id='dashboard']/div[2]/div[1]/nav/a[1]/text()").extract()
# 獲取登陸成功後頁面中的文本「Browse activity」
if 'Browse activity' in home_page:
self.logger.info('登陸成功!')
# 若是含有「Browse activity」,則打印登陸成功
else:
self.logger.error('登陸失敗!')
新建debug.py調試腳本:scrapy
# -*- coding: utf-8 -*-
from scrapy import cmdline
cmdline.execute('scrapy crawl github'.split())
修改settings.py配置文件:ide
第23行修改成:函數
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
# 遵循Robots協議