没有 next link 的 Scrapy 解析分页
Scrapy parse pagination without next link
我正在尝试解析没有下一个 link 的分页。 html 是挚爱:
<div id="pagination" class="pagination">
<ul>
<li>
<a href="//www.demopage.com/category_product_seo_name" class="page-1 ">1</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=2" class="page-2 ">2</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=3" class="page-3 ">3</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=4" class="page-4 active">4</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=5" class="page-5">5</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=6" class="page-6 ">6</a>
</li>
<li>
<span class="page-... three-dots">...</span>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=50" class="page-50 ">50</a>
</li>
</ul>
</div>
为此 html 我尝试了这个 xpath:
response.xpath('//div[@class="pagination"]/ul/li/a/@href').extract()
or
response.xpath('//div[@class="pagination"]/ul/li/a/@href/following-sibling::a[1]/@href').extract()
有没有什么好的方法来解析这个分页?谢谢大家。
PS:我也检查了这个答案:
Answer 2
一个解决方案是抓取 x 个页面,但如果页面总数不固定,这并不总是一个好的解决方案:
class MySpider(scrapy.spider):
num_pages = 10
def start_requests(self):
requests = []
for i in range(1, self.num_pages)
requests.append(scrapy.Request(
url='www.demopage.com/category_product_seo_name?page={0}'.format(i)
))
return requests
def parse(self, response):
#parse pages here.
更新
您还可以跟踪页数并执行类似操作。 a[href~="?page=2"]::attr(href)
将针对 a
个元素,其中 href
属性包含指定的字符串。 (我目前无法测试这段代码是否有效,但类似这种风格的东西应该可以)
class MySpider(scrapy.spider):
start_urls = ['https://demopage.com/search?p=1']
page_count = 1
def parse(self, response):
self.page_count += 1
#parse response
next_url = response.css('#pagination > ul > li > a[href~="?page={0}"]::attr(href)'.format(self.page_count))
if next_url:
yield scrapy.Request(
url = next_url
)
您可以简单地获取所有分页链接,并且 运行 每次您必须调用以下代码时,它都在循环中,并且选择器将返回可用的分页链接。你不必担心重复的 URL,因为 scrapy 会为你处理这个。您也可以使用 scrapy 规则。
response.css('.pagination ::attr(href)').getall()
我正在尝试解析没有下一个 link 的分页。 html 是挚爱:
<div id="pagination" class="pagination">
<ul>
<li>
<a href="//www.demopage.com/category_product_seo_name" class="page-1 ">1</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=2" class="page-2 ">2</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=3" class="page-3 ">3</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=4" class="page-4 active">4</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=5" class="page-5">5</a>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=6" class="page-6 ">6</a>
</li>
<li>
<span class="page-... three-dots">...</span>
</li>
<li>
<a href="//www.demopage.com/category_product_seo_name?page=50" class="page-50 ">50</a>
</li>
</ul>
</div>
为此 html 我尝试了这个 xpath:
response.xpath('//div[@class="pagination"]/ul/li/a/@href').extract()
or
response.xpath('//div[@class="pagination"]/ul/li/a/@href/following-sibling::a[1]/@href').extract()
有没有什么好的方法来解析这个分页?谢谢大家。
PS:我也检查了这个答案:
Answer 2
一个解决方案是抓取 x 个页面,但如果页面总数不固定,这并不总是一个好的解决方案:
class MySpider(scrapy.spider):
num_pages = 10
def start_requests(self):
requests = []
for i in range(1, self.num_pages)
requests.append(scrapy.Request(
url='www.demopage.com/category_product_seo_name?page={0}'.format(i)
))
return requests
def parse(self, response):
#parse pages here.
更新
您还可以跟踪页数并执行类似操作。 a[href~="?page=2"]::attr(href)
将针对 a
个元素,其中 href
属性包含指定的字符串。 (我目前无法测试这段代码是否有效,但类似这种风格的东西应该可以)
class MySpider(scrapy.spider):
start_urls = ['https://demopage.com/search?p=1']
page_count = 1
def parse(self, response):
self.page_count += 1
#parse response
next_url = response.css('#pagination > ul > li > a[href~="?page={0}"]::attr(href)'.format(self.page_count))
if next_url:
yield scrapy.Request(
url = next_url
)
您可以简单地获取所有分页链接,并且 运行 每次您必须调用以下代码时,它都在循环中,并且选择器将返回可用的分页链接。你不必担心重复的 URL,因为 scrapy 会为你处理这个。您也可以使用 scrapy 规则。
response.css('.pagination ::attr(href)').getall()