Scrapy FormRequest函数中的meta参数值应该如何设置? 80C

我用scrapy进行爬虫,解析函数部分另有下一级回调函数,代码如下:

item = SoccerDataItem()
for i in range(1, 8):
        item['player' + str(i + 1)] = players[i]
for j in range(1, 8):  
    home_sub_list = response.xpath('//div[@class="left"]//li[@class="pl10"]')  
        if home_sub_list[j - 1].xpath('./span/img[contains(@src,"subs_up")]'):  
            item['player' + str(j)]['name'] = home_sub_list[j - 1].xpath('./div[@class="ml10"]').xpath('string(.)').re_first('\d{1,2}\xa0\xa0(.*)')
            item['player' + str(j)]['team_stand'] = 1
            item['player' + str(j)]['is_startup'] = 0
            item['player' + str(j)]['is_subs_up'] = 1
            item['player' + str(j)]['subs_up_time'] = home_sub_list[j].xpath('./span/img[contains(@src,"subs_up")]/following-sibling::span').xpath('string(.)').extract_first(default='')
            yield scrapy.FormRequest(url=data_site, formdata=formdata, meta={'player': item['player' + str(j)]}, callback=self.parse_data)

        else:
            item['player' + str(j)]['name'] = home_sub_list[j-1].xpath('./div[@class="ml10"]').xpath('string(.)').re_first('\d{1,2}\xa0\xa0(.*)')
      item['player' + str(j)]['team_stand'] = 1
      item['player' + str(j)]['is_startup'] = 0
            item['player' + str(j)]['is_subs_up'] = 0

然而运行后一直在报错:

 callback=self.parse_data)
  File "c:\users\pc1\appdata\local\programs\python\python36-32\lib\site-packages\scrapy\http\request\form.py", line 31, in __init__
    querystr = _urlencode(items, self.encoding)
  File "c:\users\pc1\appdata\local\programs\python\python36-32\lib\site-packages\scrapy\http\request\form.py", line 66, in _urlencode
    for k, vs in seq
  File "c:\users\pc1\appdata\local\programs\python\python36-32\lib\site-packages\scrapy\http\request\form.py", line 67, in <listcomp>
    for v in (vs if is_listlike(vs) else [vs])]
  File "c:\users\pc1\appdata\local\programs\python\python36-32\lib\site-packages\scrapy\utils\python.py", line 119, in to_bytes
    'object, got %s' % type(text).__name__)
TypeError: to_bytes must receive a unicode, str or bytes object, got int

据本人百度得知,meta当中的键值对的值应为字符串,字节等类型,这正是当我传入字典类型时报错的原因。
可是,请问我应该如何修改此处呢?

PS:本人所用编程语言为Python,排版可能会引起读者不适,望谅解!

2个回答

问题不在meta,在formdata,你的formdata里面一定是有int类型的值了,改成str即可。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!