我需要從多個頁面中抓取數據。首先它應該從第一頁上抓取數據,然後從這個頁面提取一個url到第二個頁面,並從中獲取一些數據如何從同一個csv行的多個頁面中抓取數據?
所有應該在同一個csv行上。
例子是在表中第一行e.g:目錄,模型,生產和系列。
這是第二個頁面:串聯,發動機,生產日期:數據存儲 https://www.catalogs.ssg.asia/toyota/?fromchanged=true&lang=en&l=bWFya2V0PT1nZW5lcmFsfHxzdD09MzB8fHN0cz09eyIxMCI6IlJlZ2lvbiIsIjIwIjoiTWlkZGxlIEVhc3QiLCIzMCI6IjRSVU5ORVIgNjcxMzYwIn18fGNhdGFsb2c9PTY3MTM2MHx8cmVjPT1CMw%3D%3D 例子。
這是我的代碼:
import datetime
import urlparse
import socket
import scrapy
from scrapy.loader.processors import MapCompose, Join
from scrapy.loader import ItemLoader
from scrapy.http import Request
from properties.items import PropertiesItem
class BasicSpider(scrapy.Spider):
name = "manual"
# This is the page which i will hit middle est from.
start_urls = ["https://www.catalogs.ssg.asia/toyota/?fromchanged=true&lang=en"]
def parse(self, response):
# First page
next_selector ="https://www.catalogs.ssg.asia/toyota/?fromchanged=true&lang=en&l="+response.xpath('//*[@id="rows"]/tr[2]/@onclick').re(r"HM\.set\('([^']+)'")[0]
yield Request(next_selector, callback=self.parse_item)
def parse_item(self, response):
for tr in response.xpath("/html/body/table[2]/tr/td/table/tr")[1:]:
item = PropertiesItem()
item['Series']= tr.xpath("td[1]/text()").extract()
item['Engine']= tr.xpath("td[2]/text()").extract()
second_selector ="https://www.catalogs.ssg.asia/toyota/?fromchanged=true&lang=en&l="+response.xpath('/html/body/table[2]/tr/td/table/tr/@onclick').re(r"HM\.set\('([^']+)'")
yield item
def parse_item_2(self, response):
item = PropertiesItem()
item['Building_Condition']=response.xpath('/html/body/table[2]/tr/td/table/tr[2]/td[1]/text()').extract()
yield item
我需要寫在解析項目的一些代碼去parse_item_2和處理第二頁,並得到結果在同一個csv行。 如何做到這一點?