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

python3: Changing user-agent on urllib2 #6

Open
voyeg3r opened this issue Oct 9, 2017 · 0 comments
Open

python3: Changing user-agent on urllib2 #6

voyeg3r opened this issue Oct 9, 2017 · 0 comments

Comments

@voyeg3r
Copy link
Owner

voyeg3r commented Oct 9, 2017

References

from urllib.request import urlopen, Request, FancyURLopener
from bs4 import BeautifulSoup

class MyOpener(FancyURLopener):
version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'

myopener = MyOpener()
page = myopener.open('http://www.mairovergara.com/como-se-diz-cuidar-da-propria-vida-em-ingles/')
page.read()

# Até aqui o código funciona muito bem sem beautifulsoup

Another great tip came from here

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0'}

url = "https://linkedin.com/company/1005"

r = requests.get(url, headers=headers)
print(r.text)

soup = BeautifulSoup(r.text, 'html.parser')
print(soup.prettify())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant