Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python crawl website articles to save pictures locally and change the src property of HTML to local

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use python crawl website article to save the picture to the local and change the src property of HTML to the local", the content is easy to understand, clear, hope to help you solve the doubt, the following let the editor lead you to study and learn "how to use python crawl website article to save the picture locally and change the src property of HTML to the local" this article.

Every time you crawl an article, whether from csdn or other sites, the basic content is saved in a rich text editor, extracted or a html, and the image is saved on someone else's image server. What I'm going to say today is to save the picture and replace its src property with the local address. And replace it with the second, according to the original article typesetting order.

#! / usr/bin/env python

#-*-coding: utf-8-*-

# @ Time: 15:06 on 2018-11-5

# @ Author: jia.zhao

# @ Desc:

# @ File: img_test.py

# @ Software: PyCharm

Import urllib.request

Import re

Def getHtml (url):

# request via urllib

Page = urllib.request.urlopen (url)

# read the content of the page

Html = page.read ()

Return html

Def getImg (html):

Reg = rascsrc = "(. +?\ .jpg)" pic_ext'

Imgre = re.compile (reg)

# what needs to be added to Python3

Html = html.decode ('utf-8')

# find all matches

Imglist = re.findall (imgre, html)

X = 0

# Loop

For i in range (len (imglist)):

# Save the picture

# urllib.request.urlretrieve (imgurl, 'img/%s.jpg'% x)

# replace according to the src content of each picture

Html = re.sub (imglist [I], "G:/pachong/img/%s.jpg"% str (I), html)

X + = 1

Print (html)

Html = getHtml ("http://tieba.baidu.com/p/2460150866")

Print (getImg (html))

The picture in a post of this demo knowledge post bar, you can change this code according to your own needs.

The above is all the content of the article "how to use python crawl website article to save the picture locally and change the src property of HTML to local". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report