我刚开始使用 Scrapy 来抓取网站。我有 9000 多个网址要抓取。
我已经尝试过并且它起作用了,除了我想根据 url 在 json 文件中输出结果(如果我从 url1 中抓取了 10 个项目,我希望这些项目与 url1 放在一个 json 对象中,对于 url2 等也是如此) .)
{"url1": "www.reddit.com/page1",
"results1: {
["name": "blabla",
"link": "blabla",
],
["name": "blabla",
"link": "blabla",
],
["name": "blabla",
"link": "blabla",
]
},
{"url2": "www.reddit.com/page2",
"results2: {
["name": "blabla",
"link": "blabla",
],
["name": "blabla",
"link": "blabla",
],
["name": "blabla",
"link": "blabla",
]
}
是否可以这样做,还是最好先抓取整个网站,然后在工作后对其进行排序?
我现在的代码:
import scrapy
class glenmarchSpider(scrapy.Spider):
name = "glenmarch"
def start_requests(self):
start_urls = reversed([
'https://www.glenmarch.com/cars/results?make=&model=&auction_house_id=&auction_location=&year_start=1913&year_end=1916&low_price=&high_price=&auction_id=&fromDate=&toDate=&keywords=AC+10+HP&show_unsold_cars=0&show_unsold_cars=1?limit=9999',
'https://www.glenmarch.com/cars/results?make=&model=&auction_house_id=&auction_location=&year_start=1918&year_end=1928&low_price=&high_price=&auction_id=&fromDate=&toDate=&keywords=AC+12+HP&show_unsold_cars=0&show_unsold_cars=1?limit=9999'
])
for url in start_urls:
yield scrapy.Request(url, callback=self.parse)
def parse(self, response):
for caritem in response.css("div.car-item-border"):
yield {
"model": caritem.css("div.make::text").get(),
"price": caritem.css("div.price::text").get(),
"auction": caritem.css("div.auctionHouse::text").get(),
"date": caritem.css("div.date::text").get(),
"auction_url": caritem.css("div.view-auction a::attr(href)").get(),
"img": caritem.css("img.img-responsive::attr(src)").get()
}
慕容森
相关分类