How to open remote content with python?

4

In PHP, when I want to get a remote content (some url, for example), I use the proper functions to open files and this works perfectly.

Example:

file_get_contents('http://pt.stackoverflow.com/')

And in Python, what is the correct way to open a remote content?

    
asked by anonymous 13.08.2015 / 17:26

3 answers

5

Use urllib2 :

import urllib2

def file_get_contents(url):
    return urllib2.urlopen(url).read()
    
13.08.2015 / 19:38
0

This answer will depend on the Python version. The @sergiopereira response works perfectly for Python 2. But in Python 3, for example, I did not find urllib2 , but urllib3 , which is a bit different to use.

See:

import urllib3

http = urllib3.PoolManager()

response = http.request('GET', 'http://pt.stackoverflow.com/')

if response.status == 200:
    print(response.data) # resultado da página é exibido aqui
    
22.03.2017 / 19:46
0

No proxy:

import requests

requests.get('http://pt.stackoverflow.com/')

With proxy:

import requests

requests.get('http://pt.stackoverflow.com/', 
proxies={'http':'http://10.1.1.1:8080', 'https': 'https://10.1.1.1:8080'})

or

import requests
proxies_cfg={
    'http':'http://10.1.1.1:8080', 
    'https': 'https://10.1.1.1:8080',
    'ftp':'http://10.1.1.1:8061',
}
import requests
requests.get('http://pt.stackoverflow.com/', proxies=proxies_cfg)
    
27.05.2018 / 00:36