gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:50:46 24 4
gpt4 key购买 nike

因此,我试图从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(...)

关于python - 您如何处理使用Scrapable FormRequest变灰的多个下拉表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8984816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com