Created
October 18, 2012 22:53
-
-
Save macdrifter/3915232 to your computer and use it in GitHub Desktop.
get_links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get_links.py | |
# https://pythonadventures.wordpress.com/2011/03/10/extract-all-links-from-a-web-page/ | |
import re | |
import sys | |
import urllib | |
import urlparse | |
from bs4 import BeautifulSoup | |
import clipboard | |
class MyOpener(urllib.FancyURLopener): | |
version = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15' | |
def process(url): | |
myopener = MyOpener() | |
#page = urllib.urlopen(url) | |
page = myopener.open(url) | |
text = page.read() | |
page.close() | |
soup = BeautifulSoup(text) | |
for tag in soup.findAll('a', href=True): | |
tag['href'] = urlparse.urljoin(url, tag['href']) | |
print tag['href'] | |
# process(url) | |
def main(): | |
clipText = clipboard.get() | |
print clipText | |
process(clipText) | |
# main() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment