urllib

Python 3 - urllib, HTTP Error 407: Proxy Authentication Required

穿精又带淫゛_ 提交于 2019-11-28 05:31:57
I'm trying to open a website (I am behind a corporate proxy) using urllib.request.urlopen() but I am getting the error: urllib.error.HTTPError: HTTP Error 407: Proxy Authentication Required I can find the proxy in urllib.request.getproxies(), but how do I specify a username and password to use for it? I couldn't find the solution in the official docs. import urllib.request as req proxy = req.ProxyHandler({'http': r'http://username:password@url:port'}) auth = req.HTTPBasicAuthHandler() opener = req.build_opener(proxy, auth, req.HTTPHandler) req.install_opener(opener) conn = req.urlopen('http:/

Python 3- How to retrieve an image from the web and display in a GUI using TKINTER?

て烟熏妆下的殇ゞ 提交于 2019-11-28 04:54:36
问题 I want a function that, when a button is clicked, it will take an image from the web using URLLIB and display it in a GUI using TKINTER. I'm new to both URLLIB and TKINTER, so I'm having an incredibly difficult time doing this. Tried this, but it obviously doesn't work because it uses a textbox and only will display text. def __init__(self, root): self.root = root self.root.title('Image Retrieval Program') self.init_widgets() def init_widgets(self): self.btn = ttk.Button(self.root, command

How do I unit test a module that relies on urllib2?

此生再无相见时 提交于 2019-11-28 04:43:01
I've got a piece of code that I can't figure out how to unit test! The module pulls content from external XML feeds (twitter, flickr, youtube, etc.) with urllib2. Here's some pseudo-code for it: params = (url, urlencode(data),) if data else (url,) req = Request(*params) response = urlopen(req) #check headers, content-length, etc... #parse the response XML with lxml... My first thought was to pickle the response and load it for testing, but apparently urllib's response object is unserializable (it raises an exception). Just saving the XML from the response body isn't ideal, because my code uses

Python unable to retrieve form with urllib or mechanize

不羁岁月 提交于 2019-11-28 04:29:22
问题 I'm trying to fill out and submit a form using Python, but I'm not able to retrieve the resulting page. I've tried both mechanize and urllib/urllib2 methods to post the form, but both run into problems. The form I'm trying to retrieve is here: http://zrs.leidenuniv.nl/ul/start.php. The page is in Dutch, but this is irrelevant to my problem. It may be noteworthy that the form action redirects to http://zrs.leidenuniv.nl/ul/query.php. First of all, this is the urllib/urllib2 method I've tried:

urllib

不羁岁月 提交于 2019-11-28 04:19:30
https://www.cnblogs.com/strivepy/p/9231127.html urllib是python内置的http请求库(基本请求库 ,requests库就是通过urllib是实现的),各种功能相比较之下也是比较完备的,urllib库包含了一下四个模块: urllib.request 请求模块 urllib.error 异常处理模块 urllib.parse url解析模块 urllib.robotparse robots.txt解析模块 import urllib.request response=urllib.request.urlopen('http://www.baidu.com') #使用read()方法得到响应体内容,这时是一个字节流bytes,看到明文还需要decode为charset格式 print(response.read().decode('utf-8')) print(response.status) ------------------------------------- import urllib.request response=urllib.request.urlopen('https://www.python.org') print(response.read().decode('utf-8')) print

爬虫基础(未整理)

蹲街弑〆低调 提交于 2019-11-28 04:12:29
get 模式下 用到urllib模块 form urllib import request,parse URL=“http;//www.baidu.com/s?” wd= input("input your keyword") qs={“wd”:wd} qs = parse.urllencode(qs) # 第一、給输入的lkeyword从中文编码成能识别的str模式 rsp = urllib.request.urlopen(URL) #第二、打开网页 html = rsp.read()         #第三,读取html 数据 new_html = html.decode()    #第四,解码读取的数据,括号里面选填解码类型,如utf-8 print(new_html)      #最后打印html{ 【注意】前提步骤,有url,用户输入的keyword并且以dict模式贮存,再对keyword进行编码。     开始步骤 :打开网页(urlopen(url)),          读取数据(read())          数据解码(decode())         打印数据 post模式下 用到urllib模块,josn form urllib import request,parse import json url=“http://fanyi.baidu.com

how to check if the urllib2 follow a redirect?

ぐ巨炮叔叔 提交于 2019-11-28 04:04:49
问题 I've write this function: def download_mp3(url,name): opener1 = urllib2.build_opener() page1 = opener1.open(url) mp3 = page1.read() filename = name+'.mp3' fout = open(filename, 'wb') fout.write(mp3) fout.close() This function take an url and a name both as string. Then will download and save an mp3 from the url with the name of the variable name. the url is in the form http://site/download.php?id=xxxx where xxxx is the id of an mp3 if this id does not exist the site redirects me to another

Overriding urllib2.HTTPError or urllib.error.HTTPError and reading response HTML anyway

岁酱吖の 提交于 2019-11-28 03:38:25
I receive a 'HTTP Error 500: Internal Server Error' response, but I still want to read the data inside the error HTML. With Python 2.6, I normally fetch a page using: import urllib2 url = "http://google.com" data = urllib2.urlopen(url) data = data.read() When attempting to use this on the failing URL, I get the exception urllib2.HTTPError : urllib2.HTTPError: HTTP Error 500: Internal Server Error How can I fetch such error pages (with or without urllib2 ), all while they are returning Internal Server Errors? Note that with Python 3, the corresponding exception is urllib.error.HTTPError . Joe

AttributeError: 'module' object has no attribute 'urlretrieve'

荒凉一梦 提交于 2019-11-28 03:32:27
I am trying to write a program that will download mp3's off of a website then join them together but whenever I try to download the files I get this error: Traceback (most recent call last): File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py", line 214, in <module> main() File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py", line 209, in main getMp3s() File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py", line 134, in getMp3s raw_mp3.add = urllib.urlretrieve("http://www-scf.usc.edu/~chiso/oldspice/m-b1-hello.mp3") AttributeError: 'module' object has no attribute 'urlretrieve' The

Python urllib vs httplib?

被刻印的时光 ゝ 提交于 2019-11-28 03:15:13
When would someone use httplib and when urllib? What are the differences? I think I ready urllib uses httplib, I am planning to make an app that will need to make http request and so far I only used httplib.HTTPConnection in python for requests, and reading about urllib I see I can use that for request too, so whats the benefit of one or the other? urllib (particularly urllib2) handles many things by default or has appropriate libs to do so. For example, urllib2 will follow redirects automatically and you can use cookiejar to handle login scripts. These are all things you'd have to code