Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usando BeautifulSoap #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions webscraping/a.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@
# -*- coding: utf-8 -*-

import urllib2
from bs4 import BeautifulSoup
import sys

t = urllib2.urlopen('http://www.gmasson.com.br/').read()
def pega_link():
"""
Extrai os links de uma página
"""
try:
html_doc = urllib2.urlopen(sys.argv[1]).read()

# TAG
tags = t.split('<a')[1:]
tags = [ tag.split('</a>')[0].split('>',1)[1] for tag in tags ]
soup = BeautifulSoup(html_doc, 'html.parser')

for i in tags:
print i
links = soup.find_all('a')

for link in links:
print link.get('href')

except IndexError:
print "Informe um parâmetro (site alvo)"
except ValueError:
print "Informe um parâmetro no formato http://www.site.com"

if __name__ == '__main__':
pega_link()