1

The problem is about Python's CSS selectors.

I can't write a selector in the right way to select the item with "Last". I tried with:

div.pager a:[text*='Last']

Elements within which that item lies:

<div class="pager"><a href="/search/1080p/" class="current">1</a> <a href="/search/1080p/t-23/">23</a> <a href="/search/1080p/t-255/">Last</a> </div>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
SIM
  • 21,997
  • 5
  • 37
  • 109
  • 1
    I don't think that is possible with just css. [Here](https://stackoverflow.com/questions/5441680/css-selector-based-on-element-text) is a post talking about it. – Alex Bieg Jul 27 '17 at 22:02
  • Unfortunately it isn't possible to create a selector for certain text (see https://stackoverflow.com/questions/1520429/is-there-a-css-selector-for-elements-containing-certain-text). However you might be able to achieve what you want by doing `.pager a:last-child`? – joshhunt Jul 27 '17 at 22:03
  • Possible duplicate of [Is there a CSS selector for elements containing certain text?](https://stackoverflow.com/questions/1520429/is-there-a-css-selector-for-elements-containing-certain-text) – Hans Jul 27 '17 at 22:03
  • Please specify what library you're using. Python doesn't have a built-in selector library. – BoltClock Jul 28 '17 at 04:41
  • lxml library. I've updated my answer with the full script below. – SIM Jul 28 '17 at 05:15

2 Answers2

2

It is definitely possible and the answer is:

div.pager a:contains("Last")

And, here is the selector used within python script:

import requests
from lxml import html

main_link = "https://www.yify-torrent.org/search/1080p/"
base_link = "https://www.yify-torrent.org"

def get_links(item_link):
    response = requests.get(item_link).text
    tree = html.fromstring(response)
    next_page = tree.cssselect('div.pager a:contains("Next")')[0].attrib["href"]
    last_page = tree.cssselect('div.pager a:contains("Last")')[0].attrib["href"]
    print(base_link + next_page," ",base_link + last_page)

get_links(main_link)

Results:

https://www.yify-torrent.org/search/1080p/t-2/
https://www.yify-torrent.org/search/1080p/t-255/
SIM
  • 21,997
  • 5
  • 37
  • 109
1

You can't select the item with [text*='blabla']. You can only use attributes to select them.

But anyway, if you want to select the last one, you can use :last-of-type or last-child.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pacanga
  • 416
  • 6
  • 17