【问题标题】:scrapy can't submit formscrapy 无法提交表单
【发布时间】:2018-02-09 23:19:45
【问题描述】:

这是我要抓取的网页: http://www.nalpdirectory.com/Page.cfm?PageID=34。我想模拟提交表单#resultDisplayOptionsForm 并将#customDisplayNum 设置为All,这将为我带来一个包含所有列出项目的网页。

这是我的代码 sn-p:

def parse(self, response):
    yield scrapy.FormRequest.from_response(
        response,
        formid='resultDisplayOptionsForm',
        formdata={'displayNum': '100000'}, #I tried 10, 20, 30 etc. none works
        dont_click=True,
        #clickdata={'id': 'customizeDisplaySubmitBtn'},
        callback=self.after_showAll
    )
def after_showAll(self, response):
    from scrapy.shell import inspect_response
    inspect_response(response, self)

当我检查响应时,它总是显示一个失败的页面。欢迎任何建议。谢谢!

【问题讨论】:

  • 我第一次使用dont_click 参数检查某人。
  • “失败的页面”是什么意思?

标签: python html scrapy web-crawler


【解决方案1】:

这里的问题是您缺少获取数据的实际POST 请求。

仔细看,表单的POST请求url是this site,而你想要的“response”this site,所以你可以确认有什么东西不见了。

你缺少对最终站点执行第三个请求,在scrapy代码中,它会是这样的:

def parse(self, response):
    yield FormRequest.from_response(
        response,
        formid='resultDisplayOptionsForm',
        formdata={'displayNum': '100000000'},  # I tried 10, 20, 30 etc. none works
        dont_click=True,
        # clickdata={'id': 'customizeDisplaySubmitBtn'},
        callback=self.after_showAll
    )

def after_showAll(self, response):
    yield FormRequest(
        url='http://www.nalpdirectory.com/Page.cfm?PageID=34',
        formdata={
            'currPage': '1',
            'checkedFormID': '',
        },
        callback=self.parse_real,
    )

def parse_real(self, response):
    from scrapy.shell import inspect_response
    inspect_response(response, self)

【讨论】:

  • 谢谢eLRuLL!!!这是我在 StackOverflow 中的第一个问题,它得到了完美的回答。赞赏!
猜你喜欢
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多