这是我要抓取的网页: http://www.nalpdirectory.com/Page.cfm?PageID=34 .我想模拟提交表单 #resultDisplayOptionsForm 并将 #customDisplayNum 设置为 All,这将给我带来一个包含所有列出项目的网页。
这是我的代码片段:
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)
当我检查响应时,它总是显示失败的页面。欢迎提出任何建议。谢谢!
最佳答案
这里的问题是您缺少获取数据的实际 POST
请求。
如果仔细检查,表单的 POST
请求 url 是 this site ,而您想要的“响应”是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)
关于python - scrapy 无法提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45990976/