【发布时间】:2020-04-28 19:47:05
【问题描述】:
我正在努力设置从头到尾生成结果的多个页面。不幸的是,该网站没有“下一页”按钮,这使事情变得更加困难。这是网站结果https://www.imot.bg/pcgi/imot.cgi?act=3&slink=5fuby1&f1=1。
import scrapy
import logging
class ApartmentsSpider(scrapy.Spider):
name = 'apartments'
allowed_domains = ["www.imot.bg"]
start_urls = ["https://www.imot.bg/pcgi/imot.cgi?act=3&slink=5f8c9v&f1=1"
, "https://www.imot.bg/pcgi/imot.cgi?act=3&slink=5f8c9v&f1=2"]
def parse(self, response):
# apartments = response.xpath("//div[@class='price']/text()").get()
apartments = response.xpath("//a[@class='lnk1']")
for apartment in apartments:
apartment_type = apartment.xpath(".//text()").get()
apartment_link = apartment.xpath(".//@href").get()
yield response.follow(url = apartment_link, callback=self.parse_apartment, meta={"apartment_type":apartment_type,
"apartment_link" : apartment_link})
def parse_apartment(self, response):
apartment_type = response.request.meta["apartment_type"]
apartment_address = response.xpath("//form/div[2]/span[1]/text()").get()
apartment_price = response.xpath("//td/div[1]/strong[1]/text()").get()
apartment_sqm = response.xpath("//ul[@class='imotData']/li[2]/text()").get()
offered_by = response.xpath("//div/b[1]/text()").get()
contact = response.xpath("//div[8]/span[1]/span[1]/text()").get()
apartment_link = response.request.meta["apartment_link"]
yield {
"apartment_type" : apartment_type,
"apartment_address" : apartment_address,
"apartment_price" : apartment_price,
"apartment_sqm" : apartment_sqm,
"apartment_link" : apartment_link,
"offer" : offered_by,
"contact" : contact
}
【问题讨论】:
标签: web-scraping scrapy scrape