scrapy框架(https://github.com/scrapy/scrapy)提供了一個庫,供登錄需要驗證的網站時使用,https://github.com/scrapy/loginform。
我已經瀏覽了這兩個程序的文檔,但我似乎無法弄清楚如何讓scrapy在運行之前調用loginform。登錄工作正常,只有loginform。
謝謝在scrapy中使用loginform
2
A
回答
7
loginform
只是一個庫,完全與Scrapy分離。
您必須編寫代碼將其插入所需的蜘蛛中,可能是使用回調方法。
下面是做到這一點的結構的一個例子:
import scrapy
from loginform import fill_login_form
class MySpiderWithLogin(scrapy.Spider):
name = 'my-spider'
start_urls = [
'http://somewebsite.com/some-login-protected-page',
'http://somewebsite.com/another-protected-page',
]
login_url = 'http://somewebsite.com/login-page'
login_user = 'your-username'
login_password = 'secret-password-here'
def start_requests(self):
# let's start by sending a first request to login page
yield scrapy.Request(self.login_url, self.parse_login)
def parse_login(self, response):
# got the login page, let's fill the login form...
data, url, method = fill_login_form(response.url, response.body,
self.login_user, self.login_password)
# ... and send a request with our login data
return scrapy.FormRequest(url, formdata=dict(data),
method=method, callback=self.start_crawl)
def start_crawl(self, response):
# OK, we're in, let's start crawling the protected pages
for url in self.start_urls:
yield scrapy.Request(url)
def parse(self, response):
# do stuff with the logged in response
+1
第一個完整的例子,它的作品! –
3
我設法得到它的工作沒有登錄表單庫,我的解決方案如下。
import scrapy
import requests
class Spider(scrapy.Spider):
name = 'spider'
start_urls = [
'http://start.com',
]
def start_requests(self):
return [scrapy.FormRequest("login.php",
formdata={'username': 'user', 'password': 'pass'},
callback=self.start_crawl)]
def start_crawl(self, response):
#start crawling
+0
我很高興你設法讓它工作。不過,該解決方案似乎並沒有很好地與問題聯繫起來。這只是從POST請求開始抓取(不是真正的loginform範圍,即找到發送登錄POST請求的表單)。 – elias
相關問題
- 1. scrapy使用scrapy
- 2. 在Scrapy中使用子nth
- 3. 在Scrapy中使用XPath
- 4. 如何更改LoginForm中的表用戶
- 5. Vaadin LoginForm佈局 - HTML
- 6. 如何刮scrapy使用scrapy
- 7. 試圖刮Scrapy使用Scrapy
- 8. Scrapy monster.com使用scrapy框架
- 9. Yii2:自定義LoginForm不起作用
- 10. 使用scrapy
- 11. 使用scrapy
- 12. 使用硒+ Scrapy
- 13. Scrapy - 使用JOBDIR
- 14. Scrapy使用XPath
- 15. Django LoginForm中間件基於類視圖
- 16. 如何在Anaconda中使用Scrapy?
- 17. 使用Selector在Scrapy中相當於value_of_css_property?
- 18. 在Scrapy中使用Xpath進行選擇
- 19. 試圖在Scrapy中使用ItemExporter
- 20. 如何在scrapy中覆蓋/使用cookies
- 21. 在Scrapy中使用遞歸蜘蛛[Python]
- 22. 在簡單頁面中使用scrapy
- 23. 在scrapy中使用CrawlerRunner的ReactorNotRestartable錯誤
- 24. 在OS X中使用Scrapy 10.9
- 25. 在scrapy spider中使用線程
- 26. Scrapy使用Scrapy和硒的網站
- 27. 使用Scrapy的LinkExtractor
- 28. Scrapy:與使用XPath
- 29. 使用scrapy/XPath的
- 30. 無法使用scrapy
您有沒有機會嘗試我提供的解決方案? – elias
我無法工作,但是我發佈了一個解決方案。 – ollierexx