在Python,有什么之间的差异urllib,urllib2以及urllib3模块?为什么有三个?他们似乎做同样的事情......
这是我的代码:
import urllib2.request
response = urllib2.urlopen("http://www.google.com")
html = response.read()
print(html)
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
我正在尝试使用python的requests模块从Web下载并保存图像.
这是我使用的(工作)代码:
img = urllib2.urlopen(settings.STATICMAP_URL.format(**data))
with open(path, 'w') as f:
f.write(img.read())
Run Code Online (Sandbox Code Playgroud)
以下是使用以下内容的新(非工作)代码requests:
r = requests.get(settings.STATICMAP_URL.format(**data))
if r.status_code == 200:
img = r.raw.read()
with open(path, 'w') as f:
f.write(img)
Run Code Online (Sandbox Code Playgroud)
你能帮助我从响应中使用什么属性requests吗?
所以我正在尝试制作一个下载webcomics的Python脚本,并将它们放在桌面上的文件夹中.我在这里发现了一些类似的程序,但是没有什么比我需要的更好.我发现最相似的那个就在这里(http://bytes.com/topic/python/answers/850927-problem-using-urllib-download-images).我尝试使用此代码:
>>> import urllib
>>> image = urllib.URLopener()
>>> image.retrieve("http://www.gunnerkrigg.com//comics/00000001.jpg","00000001.jpg")
('00000001.jpg', <httplib.HTTPMessage instance at 0x1457a80>)
Run Code Online (Sandbox Code Playgroud)
然后我在计算机上搜索了一个文件"00000001.jpg",但我找到的只是它的缓存图片.我甚至不确定它是否将文件保存到我的电脑上.一旦我理解了如何下载文件,我想我知道如何处理剩下的文件.基本上只是使用for循环并将字符串拆分为'00000000'.'jpg'并将'00000000'递增到最大数字,我必须以某种方式确定.有关最佳方法或如何正确下载文件的任何建议吗?
谢谢!
编辑6/15/10
这是完成的脚本,它将文件保存到您选择的任何目录中.由于一些奇怪的原因,文件没有下载,他们只是做了.任何关于如何清理它的建议都将非常感激.我目前正在研究如何找到网站上存在的许多漫画,以便我可以获得最新的漫画,而不是在引发一定数量的异常后退出程序.
import urllib
import os
comicCounter=len(os.listdir('/file'))+1 # reads the number of files in the folder to start downloading at the next comic
errorCount=0
def download_comic(url,comicName):
"""
download a comic in the form of
url = http://www.example.com
comicName = '00000000.jpg'
"""
image=urllib.URLopener()
image.retrieve(url,comicName) # download comicName at URL
while comicCounter <= 1000: # not the most elegant solution
os.chdir('/file') # set …Run Code Online (Sandbox Code Playgroud) 在Python中我收到一个错误:
Exception: (<type 'exceptions.AttributeError'>,
AttributeError("'str' object has no attribute 'read'",), <traceback object at 0x1543ab8>)
Run Code Online (Sandbox Code Playgroud)
给出python代码:
def getEntries (self, sub):
url = 'http://www.reddit.com/'
if (sub != ''):
url += 'r/' + sub
request = urllib2.Request (url +
'.json', None, {'User-Agent' : 'Reddit desktop client by /user/RobinJ1995/'})
response = urllib2.urlopen (request)
jsonofabitch = response.read ()
return json.load (jsonofabitch)['data']['children']
Run Code Online (Sandbox Code Playgroud)
这个错误意味着什么,我做了什么导致它?
如何使用urllib2.urlopen上默认用户代理以外的用户代理下载网页?
使用后cgi.parse_qs(),如何将结果(字典)转换回查询字符串?寻找类似的东西 urllib.urlencode().
我试图使用Python获取URL,响应是JSON.但是,当我跑
import urllib2
response = urllib2.urlopen('https://api.instagram.com/v1/tags/pizza/media/XXXXXX')
html=response.read()
print html
Run Code Online (Sandbox Code Playgroud)
html是str类型,我期待一个JSON.有没有什么办法可以将响应捕获为JSON或python字典而不是str.
我正在运行Python 3.5.1 for Mac.我想用urllib2.我尝试安装它,但我被告知它已被拆分为urllib.request和urllib.error for Python 3.
我的命令(暂时从框架bin目录运行,因为它不在我的路径中):
sudo ./pip3 install urllib.request
Run Code Online (Sandbox Code Playgroud)
返回:
Could not find a version that satisfies the requirement urllib.request (from versions: )
No matching distribution found for urllib.request
Run Code Online (Sandbox Code Playgroud)
我在尝试一举安装urllib2之前遇到了同样的错误.
我正在尝试使用urllib2打开URL并将特定的cookie文本发送到服务器.例如,我想打开网站解决国际象棋问题,使用特定的cookie,例如search = 1.我该怎么做?
我正在尝试执行以下操作:
import urllib2
(need to add cookie to the request somehow)
urllib2.urlopen("http://chess-problems.prg")
Run Code Online (Sandbox Code Playgroud)
提前致谢
python ×10
urllib2 ×10
urllib ×3
python-3.x ×2
json ×1
python-2.7 ×1
python-2.x ×1
user-agent ×1