如何发送添加空格的 urllib2 请求 [英] How to send a urllib2 request with added white spaces

查看:36
本文介绍了如何发送添加空格的 urllib2 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送请求以打开使用空格的网页 url,以便我可以从该页面下载文件.在普通浏览器(即 chrome)中,当您在地址栏中输入 url 时,文件会自动生成并要求您下载.

I am trying to send a request to open web page url that uses white spaces so that I can download a file from the page. In a normal browser i.e chrome when you enter the url into the address bar the file is automatically generated and you are asked to download it.

每次我想要一组日志时都不必加载 Web 浏览器,而是尝试创建一个可以运行的 Python 脚本,该脚本将为我完成所有繁重的工作.

Instead of having to load a web browser every time I want a set of logs I am trying to create a python script that I can run that will do all the hard work for me.

示例:

url = http (ip-address)/supportlog.xml/getlogs&name=0335008 04-05-2013 12.46.47.zip 

我正在使用命令:

xml_page = opener.open((url))

我已经能够使用以下命令和其他一些代码行从我连接的网络服务器下载其他 zip 文件.

I have been able to to download other zip files fine from the web sever I am connecting to, using the following command and some other lines of code.

但是当我使用添加了空格的 url 尝试相同的命令时.

But when i try the same command with the url with added white spaces.

urllib2 去掉了所有的空格,这意味着我得到了一个语法错误.理想情况下,您可以将 url 更改为不包含空格,但这是不可能的.

urllib2 knocks off all of the white spaces meaning I get a syntax error back. Ideally you would change the url not to contain white spaces, but this isn't possible.

我尝试用 %20 寻址 URL 以替换空格,但这不起作用并导致服务器失败.

I have tried addressing the URL with %20 to replace the white spaces but this doesn't work and causes the sever to fail.

我知道您可以使用 urllib.quote 工具,但不确定如何使用或即使这是正确的传递方式.

I understand you can use the urllib.quote tool, but not sure how to or even if this is the correct pass to go down.

欢迎任何帮助......我仍在学习python,所以请善待.

Any help is welcome... I'm still learning python so please be kind.

推荐答案

为了清理带有空格的 url,请像这样使用 urllib.quote:

In order to clean your url with whitespaces use urllib.quote like this:

import urllib
url = urllib.quote("http://www.example.com/a url with whitespaces")

下载文件不能使用urllib2.urlopen等函数.如果您想使用 urllib 模块下载文件,您需要 urllib.urlretrieve.但是,requests 在开始时更容易掌握.

To download a file to cannot use functions like urllib2.urlopen. If you want to download a file using the urllib modules you need urllib.urlretrieve. However, requests is easier to grasp in the beginning.

import requests
response = requests.get(url)

response 提供了几个有用的功能:

The response provides several useful functions:

  • response.text:网站的源代码或下载文件的内容.
  • response.status_code:您的请求的状态代码.200 就可以了.
  • response.text: The source code of the website or the content of the downloaded file.
  • response.status_code: Status code of your request. 200 is ok.

您可能想将下载的文件保存在某处.所以用 openbinary 模式下打开一个文件连接并写入你的响应内容.不要忘记关闭文件.

You probably want to save your downloaded file somewhere. So open a file connection with open in binary mode and write the content of your response. Do not forget to close the file.

your_file_connection = open('your_file', 'wb')
your_file_connection.save(response.text)
your_file_connection.flush()
your_file_connection.close()

总结

import urllib
import requests

url = urllib.quote("http://www.example.com/a url with whitespaces")
response = requests.get(url)

your_file_connection = open('your_file', 'wb')
your_file_connection.save(response.text)
your_file_connection.
your_file_connection.close()

requests 文档:http://docs.python-requests.org/en/latest/

这篇关于如何发送添加空格的 urllib2 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆