如何使用仅带有无名密码字段的登录表单来搜索特定网页,然后将值提交给ajax?与Selenium?

问题描述 投票:0回答:1

我是Python Scrapy的新手,到目前为止:

import scrapy

class ExampleSpider(scrapy.Spider):
    name = 'example'
    allowed_domains = ['flashfurniture.com']
    start_urls = ['http://flashfurniture.com/']

    skus = ['LE-L-C-BLACK-GG', 'SZ-TUFT-SIL-GG']
    zips = ['33122', '90210', '07501', '60007']
    qtys = [1, 2, 12, 24, 50]

    def start_requests(self):
        for sku in self.skus:
            yield scrapy.Request(url='http://www.flashfurniture.com/'+sku.lower()+'.html', cookies=[{'name': 'password', 'value': 'fubar', 'domain': 'www.flashfurniture.com', 'path': '/'}], callback=self.parse)

    def parse(self, response):
        sku = response.css('#fobTable .product-code::text').extract_first()
        self.log(sku)
        for zip in self.zips:
            for qty in self.qtys:
                return scrapy.FormRequest(url=response.url, formdata={'vwquantity': str(qty), 'shippingAddressDS.shipping_ROW0_zip': zip}, callback=self.after_post)
                #self.log('LTL FDX')
                #pass #write to file

    def after_post(self, response):
        LTL = response.css('#calcBox .unique-shipment #rateRad0[value]::text').extract_first()
        FDX = response.css('#calcBox .unique-shipment #rateRad1[value]::text').extract_first()
        self.log('LTL $%s FDX $%s' % (LTL, FDX))
        self.log(response.xpath('//div[@class="show-button"][@style]'))

以上返回SKU(右转)和

2017-12-18 14:28:51 [example] DEBUG: LTL $None FDX $None
2017-12-18 14:28:51 [example] DEBUG: []
2017-12-18 14:28:51 [example] DEBUG: LTL $None FDX $None
2017-12-18 14:28:51 [example] DEBUG: []

最后一行应该是登录成功测试。

请注意,我上面代码中包含的密码无效;希望你能帮助我解决这个问题。

我想要实现的目标应该非常清楚:我需要最终写入每个SKU /页面解析的CSV五行,以包含以下内容:

SKU,Z0Q0F,Z2Q0F,Z2Q0F,Z3Q0F,Z1Q1F,Z1Q1F,Z2Q1L,Z2Q1F,Z3Q1L,Z3Q1F,Z2Q1F,Z2Q2F,Z2Q2F,Z1Q2F,Z1Q2F,Z2Q2L Z3Q2F,Z1Q3F,Z2Q3L,Z2Q4F,Z2Q4F,Z1Q4F,Z1Q4F,Z2Q4L,Z2Q4F,Z3Q4L,Z3Q4F,Z3Q4F

其中QN = qty []索引表示值,z0q0l例如=#rateRad0(LTL)返回的值,第一个zip(33122)和第一个数量(1)。

我需要计算100多种产品的每种组合的运费,请帮忙。谢谢。

我认为我应该加入Selenium但不确定如何实施。

python web-scraping scrapy scrapy-spider
1个回答
0
投票

编写代码以HTML格式保存响应正文对象。如果你所需的对象在下载的HTML文件中,那么检查你的XPath / CSS选择器,如果不是因为AJAX调用,要解决这个问题,使用SplahRequest代替scrapy。

© www.soinside.com 2019 - 2024. All rights reserved.