In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge about "object-oriented example analysis of Python language". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
First, we need to define a new HTMLParser class to override the handle_starttag() method, which we will use to display the HRef attribute values for all tags. Once you have defined your new HTMLParser class, you need to create an instance that returns Python. You can then open the HTML document using urllib.urlopen(url) and read the contents of the HTML file.
To parse the contents of an HTML file and display the links contained therein, you can pass the data to an HTMLParser object using the read() function. The HTMLParser object's feed function will receive the data and parse it accordingly through the defined HTMLParser object.
Note that if the data passed to the HTMLParser feed() function is incomplete, the incomplete tag is saved and parsed the next time the feed() function is called. This feature is useful when HTML files are large and need to be sent to the parser in segments. Here is a concrete example.
import HTMLParser import urllib import sys #Define HTML parser class parseLinks(HTMLParser.HTMLParser): def handle_starttag(self, tag, attrs): if tag == 'a': for name,value in attrs: if name == 'href': print value print self.get_starttag_text() #Create an instance of HTML parser lParser = parseLinks() #Open HTML file lParser.feed(urllib.urlopen( \ "http://www.python.org/index.html").read()) lParser.close()"Object-oriented analysis of Python language" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.