0

我练习FormRequest并遇到问题
,我在def(parse)中抓取了一个链接,我将在def(parse1)中得到一个json。
然后我得到了actIdin json,我可以产生抓取其他链接的请求,但是有这样的错误:

 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

我认为这是因为它提供了一个 jsessionidjsessionid=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'

我该怎么做才能解决这个问题?

4

1 回答 1

1

actId 的类型是什么?如果它是 int,则将 actId 从 int 转换为字符串。在最新版本的 Scarpy 中,它需要转换。

    yield FormRequest(url='http://xxx.tw/ca/toView?mtd=do', callback=self.parse3, formdata={'actId': str(actId)})  
于 2014-08-25T11:05:49.707 回答