S
S
SouLWorker2020-06-19 12:42:27
Python
SouLWorker, 2020-06-19 12:42:27

How to get a png file from a website using lxml?

The site has such a block with such a png file, how can I get it (download)?

<div class="reader-view" data-p="2">
   <img src="https://img4.example.me//example1/example2/examples/8-37/02.png">
</div>


If you know how to solve it, please explain the code a little, I still want to understand what's what.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
origami1024, 2020-06-19
@SouLWorker

from lxml import html
lxml_string = '''<div class="reader-view" data-p="2">
   <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/274px-PNG_transparency_demonstration_1.png">
</div>'''
tree = html.document_fromstring(lxml_string)
pic_path = tree.xpath('//img/@src')[0]

import requests

r = requests.get(pic_path, allow_redirects=True)
open('d:\\02.png', 'wb').write(r.content)

1. To download the file, use requests
lib 2. To parse html, use lxml.html
3. After that, using xpath technology, we find the required src attribute in the img nodes, take zero from everything found - specifically for this example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question