Using Python programming language, Implement function links() that takes as inpu
ID: 3884695 • Letter: U
Question
Using Python programming language, Implement function links() that takes as input the name of an HTML file (as a string) and returns the number of hyperlinks in that file. To do this you will assume that each hyperlink appears in an anchor tag. You also need to know that every anchor tag ends with the substring </a>.
Test your code on HTML file twolinks.html or any HTML file downloaded from the web into the folder where your program is.
CALL
>>> links( 'twolinks.html ' )
>>>2
Explanation / Answer
import urllib2 // This has the inbuilt methods
import re
def getLinks(url) //Input the URL
html_page = urllib2.urlopen() // to open th URL
Picklinks= Twolinks(html_page)
links = []
for link in picklinks.findAll('a', attrs={'href': re.compile("^http://")}): // this will get all the links
links.append(link.get('href'))
return links
)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.