python - scrapy 无法提交表单

标签 python html scrapy web-crawler

这是我要抓取的网页: 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/

相关文章:

python - 在 jupyter/iPython notebook 脚本和类方法之间同步代码

python - 如何在不使用希尔伯特索引的情况下沿希尔伯特曲线对点进行排序?

javascript - 子菜单隐藏在导航 div 中

html - 每次我将鼠标悬停在按钮上时,我的文字和图像都会移动

javascript - 是否可以使用 jQuery 隐藏匹配 html 注释上方的元素?

proxy - 如何在Scrapy中使用带密码的PROXY_LIST?

python - 使用奇数图表时,将最后一个matplotlib子图图表居中

python - 如何从 .t​​xt 文件中获取某个单词的开头和结尾

python - 在 Anaconda 中安装包并在 Python 3 中使用它

python - scrapy中额外请求的解析结果