J
J
jayrumi2017-01-12 15:30:11
Python
jayrumi, 2017-01-12 15:30:11

Python. How to get the value of the tag parameter when parsing?

Hello. When parsing the page, you need to get the value "B01CZ654Q6" from the data-asin parameter

<li id="result_1418" data-asin="B01CZ654Q6" class="s-result-item s-result-card-for-container s-carded-grid celwidget ">

What is the best way to do this?
PS I use lxml.html library

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2017-01-12
@jayrumi

>>> from lxml import etree
>>> str1 = '<li id="result_1418" data-asin="B01CZ654Q6" class="s-result-item s-result-card-for-container s-carded-grid celwidget "></li>'
>>> elem = etree.fromstring(str1)
>>> elem.get('data-asin')
'B01CZ654Q6

Actually in elem - your tag, which you pull out through xpath or whatever else ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question