Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get element by text with pyquery?

Tags:

python

pyquery

I'm writting a spider, and I want to know which link is mean "next page",so I need to get the element by the value = "next page", and then get the link. It's not only include one tag, it's a whole html source code, and I want to get the specific link.

if I want to get a element like

`<a href="http://*****">..</a>`

I can use

`'a[href^="http"]'`

And I try

`'a[text="value"]'`
like image 509
Hanson Avatar asked Oct 27 '25 06:10

Hanson


1 Answers

Try 'Contains':

from pyquery import PyQuery as pq

doc = pq("<html><body><a href='https://stackoverflow.com'>Next page</a><p>...Next time...</p></body></html>")

el = doc('a:Contains("Next")')
el.text()         # 'Next page'
el.attr['href']   # 'https://stackoverflow.com'
like image 184
John Leonard Avatar answered Oct 28 '25 19:10

John Leonard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!