This article describes how to obtain a Web Slice by using Python, and describes how to obtain a Web Slice by using a Python regular expression to match strings. For more information, see the following example. Share it with you for your reference. The details are as follows:
Here, the webpage is a section of the data collection being written. the code is as follows:
The code is as follows:
#! /User/bin/python3
Import urllib2
From HTMLParser import HTMLParser
Class MyHtmlParser (HTMLParser ):
Links = []
Def handle_starttag (self, tag, attrs ):
If tag = "img ":
If len (attrs) = 0:
Pass
Else:
For name, value in attrs:
If name = "src ":
Self. links. append (value)
If _ name _ = "_ main __":
Uri = "http://dy.163.com/v2/article/T1374483113516/AGSNE9L000964K4O"
File = urllib2.urlopen (uri). read ()
# File = "Title
I'm a paragraph!
"
Hy = MyHtmlParser ()
Hy. feed (file)
Hy. close ()
Print (hy. links)
I hope this article will help you with Python programming.