How to use python crawl website articles to save pictures locally and change the src property of HTML to local
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!