urlopen python code example
Example 1: urllib python
import urllib.request
x = urllib.request.urlopen('https://www.google.com/')
print(x.read())
Example 2: urllib.request headers
try:
from urllib.request import Request, urlopen
except ImportError:
from urllib2 import Request, urlopen
req = Request('http://api.company.com/items/details?country=US&language=en')
req.add_header('apikey', 'xxx')
content = urlopen(req).read()
print(content)
Example 3: with urllib.request.urlopen("https://
import urllib.request
req = urllib.request.Request('http://www.voidspace.org.uk')
with urllib.request.urlopen(req) as response:
the_page = response.read()
Example 4: urllib urlretrieve python 3
from urllib.request import urlretrieve
Example 5: open file in python network url
from requests_testadapter import Resp
class LocalFileAdapter(requests.adapters.HTTPAdapter):
def build_response_from_file(self, request):
file_path = request.url[7:]
with open(file_path, 'rb') as file:
buff = bytearray(os.path.getsize(file_path))
file.readinto(buff)
resp = Resp(buff)
r = self.build_response(request, resp)
return r
def send(self, request, stream=False, timeout=None,
verify=True, cert=None, proxies=None):
return self.build_response_from_file(request)
requests_session = requests.session()
requests_session.mount('file://', LocalFileAdapter())
requests_session.get('file://<some_local_path>')