I want to know how to get attribute values by specifying attribute names in XPath.

Asked 2 years ago, Updated 2 years ago, 91 views

I'm a beginner in programming and would like to do web scraping on Python.
At that time, I would like to specify the attribute name in XPath and get the attribute value, but the method is clear
I don't know.

Specifically,

<atitle="1" href="https://hogehoge.co.jp/">

If there is a tag called
and the attribute value "1" for the attribute name "title" is
I would like to get it.

What should I do?
Thank you very much for your kind instruction.

The environment is Python 3.6.4 and I use JupiterNotebook.
The browser is Google Chrome.

python python3 web-scraping xpath

2022-09-29 21:47

1 Answers

For XPath, use the

 '//a/@title'

Yes.
Example in lxml.

 from lxml.html import fromstring
el=fromstring('<a title="1" href="https://hogehoge.co.jp/">')
el.xpath('//a/@title')
['1']


2022-09-29 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.