python模拟登陆遇到重定向_python - scrapy模拟登陆知乎出现重定向无法登陆问题

当我使用scrapy想进行模拟登陆, 然后抓取首页的问题和答案时, 一直显示重定向问题 pythonfrom scrapy.contrib.spiders import CrawlSpider, Rulefrom scrapy.selector import Selectorfrom scrapy.contrib.linkextractors.sgml import SgmlLinkExtractorfrom scrapy.http import Request, FormRequestfrom zhihu.items import ZhihuItemclass ZhihuSipder(CrawlSpider) :

name = "zhihu"

allowed_domains = ["www.zhihu.com"]

start_urls = [

"http://www.zhihu.com"

]

rules = (

Rule(SgmlLinkExtractor(allow = r'http://www\.zhihu\.com/question/\d+'), callback = 'parse_page'),

)

def start_requests(self):

return [Request("https://www.zhihu.com/login", callback = self.post_login)]

#FormRequeset出问题了

def post_login(self, response):

print 'Preparing login'

xsrf = Selector(response).xpath('//input[@name="_xsrf"]/@value').extract()[0]

print xsrf

##############

return [FormRequest.from_response(response,

#"http://www.zhihu.com/login",

formdata = {

'_xsrf': xsrf,

'email': '@qq.com',

'password': '123456',

'rememberme': 'y',

},

callback = self.parse_page

)]

def parse_page(self, response):

problem = Selector(response)

item = ZhihuItem()

item['url'] = response.url

item['title'] = problem.xpath('//h2[@class="zm-item-title zm-editable-content"]/text()').extract()

item['description'] = problem.xpath('//div[@class="zm-editable-content"]/text()').extract()

item['answer']= problem.xpath('//div[@class=" zm-editable-content clearfix"]/text()').extract()

return item 使用命令运行爬虫, 可以正确打印xsrf, 但无法成功登陆 $ scrapy crawl zhihu错误结果如下2014-12-18 14:45:11+0800 [zhihu] INFO: Spider opened2014-12-18 14:45:11+0800 [zhihu] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)2014-12-18 14:45:11+0800 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:60232014-12-18 14:45:11+0800 [scrapy] DEBUG: Web service listening on 127.0.0.1:60802014-12-18 14:45:11+0800 [zhihu] DEBUG: Redirecting (301) to from 2014-12-18 14:45:11+0800 [zhihu] DEBUG: Redirecting (302) to from 2014-12-18 14:45:12+0800 [zhihu] DEBUG: Crawled (200) (referer: None)Preparing logind117e46de0dcc5e8ee2f0c7031fcafe92014-12-18 14:45:12+0800 [zhihu] DEBUG: Redirecting (302) to from 2014-12-18 14:45:12+0800 [zhihu] DEBUG: Filtered duplicate request: - no more duplicates will be shown (see DUPEFILTER_DEBUG to show all duplicates)2014-12-18 14:45:12+0800 [zhihu] INFO: Closing spider (finished)2014-12-18 14:45:12+0800 [zhihu] INFO: Dumping Scrapy stats: 希望得到解答, 为什么不能成功登陆, 非常疑惑, 非常感谢 , 上面其实已经成功登录, 后来自己测试过了, 只不过没有调用一个抓取页面url的函数, 实际上已经登录了, 在 parse_page 方法里面添加一句 print response.body 把返回的 body 复制到一个 html 文件中, 用浏览器打开,就知道了。

这里是一个简单的Python-Scrapy爬取百度搜索结果并对搜索结果进行分析的例子: 首先,我们需要安装Scrapy和lxml库。在命令行中输入以下命令: ``` pip install scrapy pip install lxml ``` 然后,我们可以创建一个名为baidu_spider的新项目,并在项目中创建一个名为baidu的新爬虫。在命令行中输入以下命令: ``` scrapy startproject baidu_spider cd baidu_spider scrapy genspider baidu www.baidu.com ``` 现在,我们在baidu_spider/spiders/baidu.py文件中编写我们的代码。我们将使用Scrapy的Selector来选择我们想要的数据。代码如下: ```python import scrapy class BaiduSpider(scrapy.Spider): name = "baidu" allowed_domains = ["www.baidu.com"] start_urls = ["http://www.baidu.com/s?wd=python"] def parse(self, response): # 获取搜索结果 results = response.xpath('//div[@class="result c-container "]') for result in results: # 获取标题和链接 title = result.xpath('.//h3/a/text()').extract_first().strip() link = result.xpath('.//h3/a/@href').extract_first() # 获取摘要 abstract = result.xpath('.//div[@class="c-abstract"]//text()').extract() abstract = "".join(abstract).strip() # 打印结果 print(title) print(link) print(abstract) ``` 在这个例子中,我们首先定义了我们的爬虫的名称,允许的域名和起始URL。然后我们定义了一个parse函数来处理响应。在parse函数中,我们使用XPath选择器来选择搜索结果。我们使用extract_first()和extract()方法来提取标题、链接和摘要。最后,我们打印了结果。 现在,我们可以在baidu_spider目录中运行以下命令来运行我们的爬虫: ``` scrapy crawl baidu ``` 这将启动我们的爬虫并开始爬取百度搜索结果。在控制台中,您应该能够看到我们的爬虫正在输出搜索结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值