我一直在尝试从 python 连接到 URL。我尝试过:urllib2、urllib3 和请求。这是我在所有情况下都遇到的相同问题。一旦我得到答案,我想他们三个都可以正常工作。
问题是通过代理连接。我已经输入了我们的代理信息,但没有任何乐趣。我收到 407 代码和错误消息,例如:HTTP 错误 407:需要代理身份验证(Forefront TMG 需要授权才能完成请求。访问 Web 代理过滤器被拒绝。)
但是,我可以使用另一个通过代理的其他应用程序进行连接,例如 git。当我运行时,git config --get htpp.proxy它返回与我在 Python 中输入的相同的值和格式
http://username:password@proxy:8080
Run Code Online (Sandbox Code Playgroud)
请求中的代码示例是
import requests
proxy = {"http": "http://username:password@proxy:8080"}
url = 'http://example.org'
r = requests.get(url, proxies=proxy)
print r.status_code
Run Code Online (Sandbox Code Playgroud)
谢谢你的时间
我已经使用 PoolManager 和 HTTPSConnectionPool 测试了“POST”请求。第一个有效,另一个给我一个:
urllib3.exceptions.MaxRetryError:
HTTPSConnectionPool(host='https://some.url.com', port=443):
Max retries exceeded with url: /some-api (Caused by <class 'socket.gaierror'>:
[Errno -2] Name or service not known)
Run Code Online (Sandbox Code Playgroud)
这是我的 PoolManager 代码:
import urllib3
HOST = 'https://some.url.com'
PORT = 443
PATH = '/some-api'
xml_request = '<some xml tree/>'
manager = urllib3.PoolManager()
res = manager.request('POST', HOST+PATH, {'req':xml_request})
Run Code Online (Sandbox Code Playgroud)
对于 HTTPSConnectonPool:
manager = urllib3.HTTPSConnectionPool(HOST, port=PORT)
res = manager.request('POST', PATH, {'req':xml_request})
Run Code Online (Sandbox Code Playgroud) 嗨,不是每次,但有时当我试图访问 LSE 代码时,我会被抛出每个烦人的 HTTP 错误 403:禁止消息。
任何人都知道我如何仅使用标准 python 模块来解决这个问题(遗憾的是没有漂亮的汤)。
import urllib.request
url = "http://www.londonstockexchange.com/exchange/prices-and-markets/stocks/indices/ftse-indices.html"
infile = urllib.request.urlopen(url) # Open the URL
data = infile.read().decode('ISO-8859-1') # Read the content as string decoded with ISO-8859-1
print(data) # Print the data to the screen
Run Code Online (Sandbox Code Playgroud)
但是,时不时地这是我显示的错误:
Traceback (most recent call last):
File "/home/ubuntu/workspace/programming_practice/Assessment/Summative/removingThe403Error.py", line 5, in <module>
webpage = urlopen(req).read().decode('ISO-8859-1')
File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 469, in open
response = meth(req, response)
File "/usr/lib/python3.4/urllib/request.py", line 579, …Run Code Online (Sandbox Code Playgroud) 我是一名新开发人员,正在学习使用 Python 3.4.2 进行编码。我在 Raspberry Pi3 上运行 Debian Linux。全新安装后我都做了
sudo apt-get update
Run Code Online (Sandbox Code Playgroud)
和
sudo apt-get upgrade
Run Code Online (Sandbox Code Playgroud)
让一切都是最新的。
我正在尝试测试将文件上传到 Dropbox 的一段代码:
import dropbox
import urllib3
authkey = (my dropbox dev auth key)
with open('test.csv','rb') as f:
dbx = dropbox.Dropbox(authkey)
dbx.files_upload(f.read(), '/test.csv')
Run Code Online (Sandbox Code Playgroud)
现在,我不知道实际的 Dropbox 代码是否正确,因为当我在 Python shell 中运行脚本时收到以下错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/requests/__init__.py", line 58, in <module>
assert minor >= 21
AssertionError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/dbtest.py", line 1, …Run Code Online (Sandbox Code Playgroud) 我正在使用urllib.requestPython 3 中的模块。
我的问题是:
如果我使用打开连接,是否需要关闭连接 urllib.request.ulropen?
如何将cookiejar与 urllib3 一起使用?
根据Python urllib3,如何处理cookie支持?从2010年开始就不支持了。但是,在urllib3 1.22 (2017-07-20)的更改日志中提到了 cookiejar 的兼容性修复。
修复了 cookiejar 的兼容性。(问题#1229)
所以听起来好像添加了一些支持。但我在 urllib3 的文档中找不到任何关于 cookiejar 甚至 cookies 的提及。
(感谢您查看此内容!)
我正在尝试使用 python3 和简单的 urllib3 http.request 从https://login.morganstanleyclientserv.com读取 HTML 。
看起来服务器正在重置连接,最终 urllib3 的重试放弃。
这里是否存在 TLS 协商问题?如果是这样,urllib3如何补偿?
或者问题出在其他地方?如何解决这个问题?
我已经尝试使用curl进行相同的(?)事务...它返回预期的HTML,没有任何延迟。
我还尝试从不同的站点阅读(例如,https://client.schwab.com/Login/SignOn/CustomerCenterLogin.aspx)...没问题。
Chrome 加载https://login.morganstanleyclientserv.com没有问题。
uname -a ; python3 -V 返回:
Linux ubuntu 4.18.0-17-generic #18~18.04.1-Ubuntu SMP 3月15日星期五15:27:12 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux Python 3.6.7
这是有效的卷曲:
curl -v --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" --header "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" --header "Accept-Encoding: text/plain" --header "Accept-Language: en-US,en;q=0.9" --output foo https://login.morganstanleyclientserv.com
Run Code Online (Sandbox Code Playgroud)
这是挂起的 python3 + urllib3 代码(打印 1 后,然后打印 …
每当我想在 amazon.com 上抓取内容时,我都会失败。因为产品信息会根据 amazon.com 中的位置而变化
本次变更信息如下;
用selenium改变位置很简单,但是处理速度很慢。这就是为什么我需要使用 scrapy 或 requests 进行抓取。
然而,虽然我模仿浏览器中的cookie和标头,但amazon.com不允许我更改位置。
有两个大问题。
非常有趣的是,没有关于这个主题的信息。您无法对世界第一的购物网站进行定位抓取。
请知道这个问题答案的人赐教。如果有scrapy或者requests这样的解决方案就足够了。说实话,我已经一年没有解决这个问题了。
import requests
from lxml import etree
from random import choice
from urllib3.exceptions import InsecureRequestWarning
import urllib.parse
import urllib3.request
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def location():
headersdelivery = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36',
'content-type':'application/x-www-form-urlencoded',
'accept':'text/html,*/*',
'x-requested-with':'XMLHttpRequest',
'contenttype':'application/x-www-form-urlencoded;charset=utf-8',
'origin':'https://www.amazon.com',
'sec-fetch-site':'same-origin',
'sec-fetch-mode':'cors',
'sec-fetch-dest':'empty',
'referer':'https://www.amazon.com/',
'accept-encoding':'gzip, deflate, br',
'accept-language':'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7'
}
payload …Run Code Online (Sandbox Code Playgroud) 尝试使用urllib3发布JSON编码的数据.只是希望我的POST有效负载是原始JSON字符串,内容类型为application/json.我只是看不出怎么做.
urllib3文档描述了在"字段"中发布数据,即具有(键,值)对的字典,就像HTML表单是如何使用URL进行URL编码的.但我不想这样做.
我能得到的最接近的是(我只是猜到了数据的放置位置,因为它没有记录在我能找到的任何地方):
http = urllib3.PoolManager()
headers = urllib3.util.make_headers(basic_auth=key+":")
r = http.request_encode_body('POST', path, json.dumps(payload), headers=headers)
Run Code Online (Sandbox Code Playgroud)
这导致这个urllib3错误:
File "C:\Python27\lib\site-packages\urllib3-1.7.1-py2.7.egg\urllib3\filepost.py", line 44, in iter_field_objects
yield RequestField.from_tuples(*field)
TypeError: from_tuples() takes exactly 3 arguments (2 given)
Run Code Online (Sandbox Code Playgroud)
感谢您的任何指示!
我一直在尝试向API发出请求,我必须通过以下正文:
{
"description":"Tenaris",
"ticker":"TS.BA",
"industry":"Metalúrgica",
"currency":"ARS"
}
Run Code Online (Sandbox Code Playgroud)
尽管代码似乎是正确的,并且它以"处理完成退出代码0"结束,但它运行不正常.我不知道我错过了什么,但这是我的代码:
http = urllib3.PoolManager()
http.urlopen('POST', 'http://localhost:8080/assets', headers={'Content-Type':'application/json'},
data={
"description":"Tenaris",
"ticker":"TS.BA",
"industry":"Metalúrgica",
"currency":"ARS"
})
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这是使用Python的第一天,如果我不够具体,请原谅我.
urllib3 ×10
python ×7
python-3.x ×4
urllib ×2
bash ×1
curl ×1
dropbox-api ×1
https ×1
json ×1
linux ×1
post ×1
proxy ×1
python-2.x ×1
scrapy ×1
web-scraping ×1