Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scrapy-基于文本选择特定链接_Python_Web Crawler_Scrapy - Fatal编程技术网

Python Scrapy-基于文本选择特定链接

Python Scrapy-基于文本选择特定链接,python,web-crawler,scrapy,Python,Web Crawler,Scrapy,这应该很容易,但我被卡住了 <div class="paginationControl"> <a href="/en/overview/0-All_manufactures/0-All_models.html?page=2&amp;powerunit=2">Link Text 2</a> | <a href="/en/overview/0-All_manufactures/0-All_models.html?page=3&amp

这应该很容易,但我被卡住了

<div class="paginationControl">
  <a href="/en/overview/0-All_manufactures/0-All_models.html?page=2&amp;powerunit=2">Link Text 2</a> | 
  <a href="/en/overview/0-All_manufactures/0-All_models.html?page=3&amp;powerunit=2">Link Text 3</a> | 
  <a href="/en/overview/0-All_manufactures/0-All_models.html?page=4&amp;powerunit=2">Link Text 4</a> | 
  <a href="/en/overview/0-All_manufactures/0-All_models.html?page=5&amp;powerunit=2">Link Text 5</a> |   

<!-- Next page link --> 
  <a href="/en/overview/0-All_manufactures/0-All_models.html?page=2&amp;powerunit=2">Link Text Next ></a>
</div>

例如,我想选择下一页链接,因为它的文本是“linktext next”。有什么想法吗?

您的xpath正在选择href,而不是
a
标记中的文本。从您的示例看,href中没有
next
,因此您无法通过RE找到它。

使用
a[contains(text(),'Link text next')]

参考:关于XPath函数的文档


注:您的文本
下一步链接文本
末尾有一个空格。为避免在代码中包含该空格,请执行以下操作:

text()="Link Text Next "

我认为使用
contains
更为通用,但仍然足够具体。

您可以使用以下XPath表达式:

//div[@class='paginationControl']/a[text()="Link Text Next"]/@href
这将选择带有文本的链接的
href
属性
“下一步链接文本”


看看你们是否需要更多的控制。

谢谢大家。我使用了@unutbu的建议,效果非常好。所以没有CSS选择器方法可以做到这一点?
text()="Link Text Next "
//div[@class='paginationControl']/a[text()="Link Text Next"]/@href