Python 无法创建适当的CSS选择器
问题在于Python的CSS选择器 我无法以正确的方式编写选择器来选择带有“Last”的项。我试过:Python 无法创建适当的CSS选择器,python,css-selectors,lxml,Python,Css Selectors,Lxml,问题在于Python的CSS选择器 我无法以正确的方式编写选择器来选择带有“Last”的项。我试过: div.pager a:[text*='Last'] 项目所在的元素: <div class="pager"><a href="/search/1080p/" class="current">1</a> <a href="/search/1080p/t-23/">23</a> <a href="/search/1080p/t-2
div.pager a:[text*='Last']
项目所在的元素:
<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>
您不能使用
[text*='blablabla']
选择项目。只能使用属性来选择它们
但无论如何,如果要选择最后一个,可以使用
:last of type
或last child
这是绝对可能的,答案是:
div.pager a:contains("Last")
下面是python脚本中使用的选择器:
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)
结果:
https://www.yify-torrent.org/search/1080p/t-2/
https://www.yify-torrent.org/search/1080p/t-255/
我认为仅仅使用css是不可能的。是一篇关于它的帖子。不幸的是,无法为某些文本创建选择器(请参阅)。但是,通过执行
,您可能能够实现您想要的。寻呼机a:最后一个孩子
?的可能副本请指定您正在使用的库。Python没有内置的选择器库。lxml库。我已经用下面的完整脚本更新了我的答案。使用CSS是不可能的,你可以用这种方式使用jquery。你搞错了。我在python脚本中将其用作css选择器。然后请编辑您的帖子,提供一些详细信息,并添加一个标记。或者我将编辑它^ ^)