【发布时间】:2021-12-24 21:37:34
【问题描述】:
我正在编写一个爬虫爬虫来爬取房地产网站 Rightmove。我遇到的问题是,由不同房屋列表的几页组成的房产搜索都位于同一个 URL 下。
这意味着识别“下一页”的 URL 的常规过程不起作用。有什么方法可以使用scrapy而不是selenium(对于此目的不够有效),我可以浏览不同的页面?请看我的代码和相关“下一页”按钮的源代码,如下图所示。
谢谢。
class listingsSpider(scrapy.Spider):
name = 'listings'
start_urls = ['https://www.rightmove.co.uk/property-for-sale/find.html?locationIdentifier=STATION%5E1712&maxPrice=500000&radius=0.5&sortType=10&propertyTypes=&mustHave=&dontShow=&furnishTypes=&keywords=']
def parse(self, response):
self.logger.info('This my first spider')
address = response.xpath('//*[@id="property-65695633"]/div/div/div[4]/div[1]/div[2]/a/address')
listings = response.xpath('//h2[@class="propertyCard-title"]')
for listing in listings:
yield{
'Listing': listing.get()
}
nextPage = response.xpath('//*[@id="l-container"]/div[3]/div/div/div/div[3]/button/div/svg/use')
nextPage = nextPage.get()
pageTest = response.css('div[class=pagination-button pagination-direction pagination-direction--next] svg a::attr(href)')
pageTest = pageTest.get()
if pageTest is not None:
pageTest = response.urljoin(pageTest)
yield scrapy.Request(pageTest,callback=self.parse)
```[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/1I1J1.png
【问题讨论】:
-
在scrapy shell中如果你输入view(response)你不会得到任何分页。最好单击下一个并找到下一个网址,然后从 start_requests 开始
标签: python html python-3.x scrapy