Python How to get all the links from a website


SUBMITTED BY: alemotta

DATE: April 1, 2017, 11:11 p.m.

FORMAT: Text only

SIZE: 448 Bytes

HITS: 1193

  1. Python How to get all the links from a website
  2. This example will get all the links from any websites HTML code.
  3. To find all the links, we will in this example use the urllib2 module together
  4. with the re.module
  5. import urllib2
  6. import re
  7. #connect to a URL
  8. website = urllib2.urlopen(url)
  9. #read html code
  10. html = website.read()
  11. #use re.findall to get all the links
  12. links = re.findall('"((http|ftp)s?://.*?)"', html)
  13. print links

comments powered by Disqus