Scrapy - Select specific link based on text

我只是一个虾纸丫 提交于 2019-12-06 23:09:05

问题


This should be easy but I'm stuck.

<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>

I'm trying to use Scrapy (Basespider) to select a link based on it's Link text using:

nextPage = HtmlXPathSelector(response).select("//div[@class='paginationControl']/a/@href").re("(.+)*?Next")

For example, I want to select the next page link based on the fact that it's text is "Link Text Next". Any ideas?


回答1:


Use a[contains(text(),'Link Text Next')]:

nextPage = HtmlXPathSelector(response).select(
    "//div[@class='paginationControl']/a[contains(text(),'Link Text Next')]/@href")

Reference: Documentation on the XPath contains function


PS. Your text Link Text Next has a space at the end. To avoid having to include that space in the code:

text()="Link Text Next "

I think using contains is a bit more general while still being specific enough.




回答2:


You can use the following XPath expression:

//div[@class='paginationControl']/a[text()="Link Text Next"]/@href

This selects the href attributes of the link with text "Link Text Next".

See XPath string functions if you need more control.




回答3:


Your xpath is selecting the href not the text in the a tag. It doesn't look from your example like the href has next in it, so you can't find it with an RE.



来源:https://stackoverflow.com/questions/12145067/scrapy-select-specific-link-based-on-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!