0
我正在從Factiva獲取數據,使用Python 3.5.2。我必須使用學校登錄信息才能看到數據。如何使用python Scrapy抓取Factiva數據?
我按照這個帖子來嘗試創建login spider
這是我的代碼:
# Test Login Spider
import scrapy
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
login_url = "https://login.proxy.lib.sfu.ca/login?qurl=https%3a%2f%2fglobal.factiva.com%2fen%2fsess%2flogin.asp%3fXSID%3dS002sbj1svr2sVo5DEs5DEpOTAvNDAoODZyMHn0YqYvMq382rbRQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQQAA"
user_name = b"[my_user_name]"
pswd = b"[my_password]"
response_page = "https://global-factiva-com.proxy.lib.sfu.ca/hp/printsavews.aspx?pp=Save&hc=All"
class MySpider(scrapy.Spider):
name = 'myspider'
def start_requests(self):
return [scrapy.FormRequest(login_url,
formdata={'user': user_name, 'pass': pswd},
callback=self.logged_in)]
def logged_in(self, response):
# login failed
if "authentication failed" in response.body:
print ("Login failed")
# login succeeded
else:
print ('login succeeded')
# return Request(url=response_page,
# callback=self.parse_responsepage)
def parse_responsepage(self, response):
hxs = HtmlXPathSelector(response)
yum = hxs.select('//span/@enHeadline')
def main():
test_spider = MySpider(scrapy.Spider)
test_spider.start_requests()
if __name__ == "__main__":
main()
爲了辦好這代碼,我在項目的頂層目錄中使用了終端命令行:
scrapy runspider [my_file_path]/auth_spider.py
你知道如何處理這裏的錯誤嗎?
噢,我的上帝,它顯示登錄成功。我以爲我永遠無法解決這個問題....非常感謝你! –