Python's method of extracting hyperlinks from web pages

  • 2020-05-10 18:25:17
  • OfStack

The easiest way to do this is to grab the target page back and get the hyperlink by matching the href attribute in the a tag

The code is as follows:


import urllib2
import re
 
url = 'http://www.sunbloger.com/'
 
req = urllib2.Request(url)
con = urllib2.urlopen(req)
doc = con.read()
con.close()
 
links = re.findall(r'href\=\"(http\:\/\/[a-zA-Z0-9\.\/]+)\"', doc)
for a in links:
  print a

conclusion

Above is the whole content of this article, I hope the content of this article can help you in your study or work, if you have any questions, you can leave a message to communicate.


Related articles: