scrapy:FormRequest 是否有 jsessionid
我在练习使用FormRequest的时候遇到了一个问题。
首先,我在一个叫做def(parse)的地方爬取了一个链接,然后在另一个叫做def(parse1)的地方收到了一个json格式的返回数据。
接着,我从这个json中得到了一个叫做actId
的值,这个值可以用来发送请求去爬取其他链接,但我遇到了这样的错误:
ERROR: Spider error processing <POST http://xxx.tw/ca/to.do;jsessionid=A69C5203A49A12DA450F32E6B2AB0E23?mtd=Search&mId=604>
exceptions.TypeError: unicode_to_str must receive a unicode or str object, got int
我觉得这个问题可能是因为它传递了一个jsessionid,内容是jsessionid=A69C5203A49A12DA450F32E6B2AB0E23
。
因为我尝试过用yield FormRequest(url='http://xxx.tw/ca/toView?mtd=do', callback=self.parse3, formdata={'actId': actId})
,结果是成功的。
这里是代码:
def parse(self, response):
yield FormRequest.from_response(response,
formname='Form',
formdata={'when': '9',
'key': 'please input',
},
callback=self.parse1)
#<form name="Form" id="search" method="post" action="/ca/to?mtd=do&Id=4">
def parse1(self, response):
data = json.loads(response.body)
tryone = data.get('to')
for i in tryone:
actId = i['actId']
yield FormRequest(url='http://xxx.tw/ca/toView?mtd=do', callback=self.parse3, formdata={'actId': actId})
def parse3(self, response):
print response.status #200
print 'haha'
我该怎么做才能解决这个问题呢?
1 个回答
1
actId是什么类型的呢?如果它是整数(int),那么就需要把actId从整数转换成字符串(string)。在最新版本的Scrapy中,这个转换是必须的。
yield FormRequest(url='http://xxx.tw/ca/toView?mtd=do', callback=self.parse3, formdata={'actId': str(actId)})