python - 您如何处理使用Scrapable FormRequest变灰的多个下拉表单

因此,我试图从Gasbuddy.com上删除一些汽车信息,但是我在使用这些易破解的代码时遇到了一些麻烦。

这是我到目前为止所拥有的,请让我知道我做错了什么:

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.loader import XPathItemLoader
from scrapy.http import Request
from scrapy.http import FormRequest

class gasBuddy(BaseSpider):
name = "gasBuddy"
allowed_domains = ["http://www.gasbuddy.com"]
start_urls = [
    "http://www.gasbuddy.com/Trip_Calculator.aspx",
]

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    #for years in hxs.select('//select[@id="ddlYear"]/option/text()'):
        #print years
    FormRequest(url="http://www.gasbuddy.com/Trip_Calculator.aspx",
                formdata={'Year': '%s'%("2011")},
                callback=self.make('2011'))


def make (years, self, response):
    #this is where we loop through all of the car makes and send the response to modle
    hxs = HtmlXPathSelector(response)
    for makes in hxs.select('//select[@id="ddlMake"]/option/text()').extract()
        FormRequest(url="http://www.gasbuddy.com/Trip_Calculator.aspx",
                formdata={'Year': '%s', 'Make': '%s'%(years, makes)},
                callback=self.model(years, makes))


def model (years, makes, self, response):
    #this is where we loop through all of the car modles and get all of the data assoceated with it.
    hxs = HtmlXPathSelector(response)
    for models in hxs.select('//select[@id="ddlModel"]/option/text()')
        FormRequest(url="http://www.gasbuddy.com/Trip_Calculator.aspx",
                formdata={'Year': '%s', 'Make': '%s', 'Model': '%s'%(years, makes, models)},
                callback=self.model(years, makes))

        print hxs.select('//td[@id="tdCityMpg"]/text()')

我用这段代码的基本思想是选择一个表单字段,然后调用formRequest并调用另一个函数,然后该函数继续循环直到我到达最后一个函数,然后开始阅读每辆汽车的信息。但我不断收到一些错误...一个被
gasbuddy没有属性“编码”(我不知道那是什么)。
我也不确定是否可以将边界传递给回调函数。

任何帮助将不胜感激。

最佳答案

该答案仅涵盖使用附加参数调用回调的方法,而不能解决具体站点的动态表单问题。

要将其他参数传递给回调,可以使用标准Python库中的 functools.partial

没有Scrapy的简化示例:

import functools


def func(self, response):
    print self, response

def func_with_param(self, response, param):
    print self, response, param    

def caller(callback):
    callback('self', 'response')

caller(func)
caller(functools.partial(func_with_param, param='param'))

因此,您应该像这样定义makemodel函数(self始终是第一个参数):
def make (self, response, years):
    ...

def model (self, response, years, makes):
    ...

和回调参数:
import functools
...

def parse(self, response):
    ...
    return FormRequest(url="http://www.gasbuddy.com/Trip_Calculator.aspx",
                       formdata={'Year': '%s'%("2011")},
                       callback=functools.partial(self.make, years='2011'))

在Scrapy中将参数传递给回调的另一种方法是对meta使用 FormRequest 参数

例如。:
def parse(self, response):
    ...
    return FormRequest(url="http://www.gasbuddy.com/Trip_Calculator.aspx",
                       formdata={'Year': '%s'%("2011")},
                       meta={'years':'2011'},
                       callback=self.make)

def make (self, response):
    years = response.meta['years']
    ...

models类似。

代码中的另一个问题是FormRequest,仅创建而不使用。您应该返回它们(例如在我的parse示例中)或在for循环中将它们yield:
for something in hxs.select(...).extract():
    yield FormRequest(...)

https://stackoverflow.com/questions/8984816/

相关文章:

python - python程序显示编译错误

c# - 无法将类型 'IEnumerable'隐式转换为 'bool'

perl - 使用中间变量访问电子表格::Read sheets时出错

objective-c - Objective-C ISO C++禁止声明无类型的X

syntax - D中的基本运算符重载(第2部分)

c# - C#—在Convert.ToInt32(...)之后,循环将不接受非数字输入

f# - 'NS.Type1'类型与f#中的 'NS.Type1

objective-c - 运行奇怪的错误运行 objective-c 代码

c# - 如何修复显示 “The type or namespace name X does not

python - 我类的数组给我一个错误……AttributeError : 'set' objec