簡體   English   中英

python spider scrapy 無法啟動代碼

[英]python spider scrapy cannot launch the code

我之前用過Selenium,但是現在客戶需要Scrapy框架才能在他的項目中使用。

我閱讀和觀看。 我談到了如何編寫第一個請求蜘蛛的一些要點。 但我需要更多的幫助。

import scrapy


class QuotesSpider(scrapy.Spider):
    name = 'quotes'
    plate_num = "EA66LEE"
    start_urls = [
        f'https://dvlaregistrations.dvla.gov.uk/search/results.html?search={plate_num}&action=index&pricefrom=0&priceto=&prefixmatches=&currentmatches=&limitprefix=&limitcurrent=&limitauction=&searched=true&openoption=&language=en&prefix2=Search&super=&super_pricefrom=&super_priceto='
,
    ]


    def parse(self, response):
        for quote in response.xpath('div[@class="resultsstrip"]/a/p'):
            yield {
                'plate number': plate_num,
                'price': quote.xpath('div[@class="resultsstrip"]/a/p[@class="resultsstripprice"/text()]').get(),
            }

我想刮url如果車牌號存在然后抓住

web元價簽。

<a id="buy_EA66LEE" class="resultsstripplate plate" href="/buy.html?plate=EA66 LEE&amp;price=999" title="Buy now">EA66 LEE                              </a>
<p class="resultsstripprice">£999</p>

即使從終端我也無法從位於response.xpath('div/a/p/text()').get() xpath 獲得正確的值

您需要將基數添加到 xpath 表達式中。 xpath 路徑應始終以/./開頭,它們代表絕對或相對 xpath 路徑。 在您的情況下,您可以獲得所有價格。

response.xpath('//p[@class="resultsstripprice"]/text()').getall()

上面的路徑是一個絕對路徑,它查找所有 class 屬性為"resultsstripprice" resultsstripprice”的<p>標簽,並提取標簽的文本內容。

此頁面xpath語法和符號的良好資源/參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM