[英]Scrapy Next Page Button and Previous Page Button are on the same class, can't reach the next page
I am trying to scrape one dictionary.我正在尝试刮一本字典。 Which has next page and previous page buttons.其中有下一页和上一页按钮。 When I try to reach next page("Sonraki Sayfa") with this way当我尝试以这种方式到达下一页(“Sonraki Sayfa”)时
next_page = response.css('div.col-md-6.col-sm-6.col-xs-6 a::attr(href)').get()
I always reach the previous page button because they have same class names.我总是到达上一页按钮,因为它们具有相同的 class 名称。
This is the html code of website:这是网站的 html 代码:
<ul class="sayfalama">
<div class="col-md-12 col-xs-12 col-sm-12">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6">
<a href="kelimeler.php?s=-1" style="background: white; font-weight: bold; padding:5px;">Önceki Sayfa</a>
</div>
<div class="col-md-6 col-sm-6 col-xs-6">
<a href="kelimeler.php?s=1" style="background: white; font-weight: bold; padding:5px;">Sonraki Sayfa</a>
</div>
</div>
</ul>
This is my code of spider这是我的蜘蛛代码
next_page = response.css('div.col-md-6.col-sm-6.col-xs-6 a::attr(href)').get()
print(next_page)
if next_page is not None:
yield response.follow(next_page, callback = self.parse)
What should change to reach next page(Sonraki Sayfa) instead of previous page( Onceki Sayfa)?应该改变什么才能到达下一页(Sonraki Sayfa)而不是上一页(Onceki Sayfa)?
You can try with nth-child
.您可以尝试使用nth-child
。 like below:如下所示:
next_page = response.css('div.col-md-6.col-sm-6.col-xs-6:nth-child(2) a::attr(href)').get()
div.col-md-6.col-sm-6.col-xs-6:nth-child(2) a { color:red; }
<div class="col-md-6 col-sm-6 col-xs-6"> <a href="kelimeler.php?s=-1" style="background: white; font-weight: bold; padding:5px;">Önceki Sayfa</a> </div> <div class="col-md-6 col-sm-6 col-xs-6"> <a href="kelimeler.php?s=1">Sonraki Sayfa</a> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.