We have compiled most frequently asked Python Interview Questions which will help you with different expertise levels.
Python Interview Questions on HTML/XML in Python
What module is used to handle URLs?
Illustrate parsing a URL into a tuple.
myTuple = urlparse.urlparse( “http://wivzv.example.com/mydirectory/ myfile.html”)
What modules does Python provide for opening and fetching data from URLs?
urllib and urllib2.
What is the main difference between the urllib and urllib2 modules?
The urllib2 module can accept a Request object, which allows the specification of headers.
Illustrate opening and printing a web-based URL.
import urllib ,
myBuffer = myURL.read( )
- How to Download an Image from a URL in Python?
- Python Interview Questions on File Manipulation
- Python Selenium Tutorial | Selenium Python Training for Beginners
How is the requested information retrieved?
Using the .info( ) method on an open URL.
To process the contents of an HTML file, what module is used?
How is the HTMLParser used?
By subclassing the HTMLParser.HTMLParser class, and inserting processing for the tags of interest, instantiating it, then by calling the .feed method of the resulting object.
What modules are used to manage cookies?
The urllib and cookielib modules.
Illustrate retrieving a cookies from a URL.
myjar= cookielib.LWPCookieJar( )
myOpener = urllib2.build_opener(
urllib2. ins tall_opener( myOpener)
myRequest =urllib2. RequestC’http:!/www.example.org/”)
myHTML = urllib2.urlopen(my Request)
for cookie in enumerate(myjar)
In managing XML documents, what is the module most often used?
xml.dom Also, the minidom class within xml.dom
Illustrate opening an xml document.
from xml.dom import minidom myTree = minidom.parse(‘myXML.xml’) print myTree.toxmlO
What tools are available to determine if an xml file is well formed?
Using a try, except block, and attempting to parse the xml file through the generic parser available in xml.sax
How are xml element attributes accessed?
Using the minidom parser, then the .getAttribute method on returned child nodes.
What method is used to determine if a child node includes a particular attribute?
Using the .hasAttribute method which will return true if the attribute is defined.
What is the difference between the .toxml and .toprettyxml methods?
The .toprettyxml method will indent the node contents appropriately.
What is expat module and what is it used for?
The expat module is a non-validating XML parser. It is used to process XML very quickly, with minimal regard for the formal correctness of the XML being parsed.
What does the .dom stand for in the xml.dom module?
Document Object Model.
What parsers are available for XML?
Sax, expat, and minidom
Illustrate direct node access in an XML file.
from xml.dom import minidom myXML = minidom.parse(”myXML.xml”)
myNodes = myXML.childNode s print childNodes.toprettyxml()