【问题标题】:Error formatting the body of a scrapy request格式化scrapy请求的正文时出错
【发布时间】:2022-01-02 14:38:04
【问题描述】:

当我在没有格式化正文的情况下发出一个scrapy请求时,我得到了正确的结果,但是,当我格式化它以进行循环时,我得到一个 400 错误。 这是未格式化的正文: '{"fields":"id,angellist_url,job_roles","limit":25,"offset":0,"form_data":{"must":{"filters":{"founding_or_hq_slug_locations":{"values":["spain"],"execution":"or"}},"execution":"and"},"should":{"filters":{}},"must_not":{"growth_stages":["mature"],"company_type":["service provider","government nonprofit"],"tags":["outside tech"],"company_status":["closed"]}},"keyword":null,"sort":"-last_funding_date"}'

这是格式化的正文:

'{"fields":"id,angellist_url,job_roles","limit":25,"offset":{offset_items},"form_data":{"must":{"filters":{"founding_or_hq_slug_locations":{"values":["spain"],"execution":"or"}},"execution":"and"},"should":{"filters":{}},"must_not":{"growth_stages":["mature"],"company_type":["service provider","government nonprofit"],"tags":["outside tech"],"company_status":["closed"]}},"keyword":null,"sort":"-last_funding_date"}'

然后在提出请求时我使用:

yield scrapy.Request(url = url, headers = headers, body = body.format(offset_items = '0'))

【问题讨论】:

  • 你能给我们举个例子,说明{**offset_items**} 里面有什么,因为其他一切看起来都一样吗?当您说“格式”时,您的意思是您使用 json 序列化数据吗?当您说“制作循环”时,您的意思是您正在尝试制作数据的 for 循环并将其放入嵌套对象内的 offset items 中?
  • offset_items 内部的一个例子是 0。是的,我的意思是循环数据并将其放入 offset_items

标签: python scrapy request


【解决方案1】:

如果不是 **offset_items** 在导致问题的“格式化”字符串中,两者是相同的。 Scrapy 需要一个 int 作为偏移量。

您尝试使用 .format() 将 offset kwarg 值传递给正文,但使用了错误的语法。最好的解决方案是使用 f-string 来传递值。

您可以在python中使用difflib之类的东西来快速检查两个字符串之间的差异,这样您就可以轻松解决此类问题

【讨论】:

  • 在这种情况下如何使用 f-string 传递值,offset_items = 0?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 1970-01-01
  • 2021-12-06
  • 2021-07-22
  • 2018-09-19
相关资源
最近更新 更多