在Python爬虫中,如果你只想爬取第一个页面,你可以采用以下方法:
使用`requests.get`并取消后续请求
import requests发送 GET 请求获取第一个页面response = requests.get("https://example.com")取消所有后续请求response.close()
使用`scrapy.Request`并在`callback`中返回`None`
import scrapyclass MySpider(scrapy.Spider):name = "my_spider"start_urls = ["https://example.com"]def parse(self, response):处理第一个页面内容...返回 None 停止爬取后续页面return None

在`scrapy.Spider`中重写`start_requests`方法
import scrapyclass MySpider(scrapy.Spider):name = "my_spider"start_urls = ["https://example.com"]def start_requests(self):发送第一个请求yield scrapy.Request(self.start_urls, self.parse)def parse(self, response):处理第一个页面内容...返回 None 停止爬取后续页面return None
以上方法可以帮助你实现只爬取第一个页面的需求。
