连接到互联网?

时间:2017-01-11 17:39:18

标签: python python-3.x ctypes wininet

我在使用python连接到Internet时遇到问题。

我在使用PAC文件设置代理的公司网络上。现在,如果我能找到并解析PAC以获得我需要的东西,那就没关系了。但是我不能。

怪异:

R可以连接到互联网,通过wininet和.External(C_download,...)下载文件,所以我知道这是可能的,当我这样做时:

import ctypes

wininet = ctypes.windll.wininet
flags = ctypes.wintypes.DWORD()
connected = wininet.InternetGetConnectedState(ctypes.byref(flags), None)
print(connected, hex(flags.value))

我得到:1 0x12所以我有一个可用的连接,但是一旦我尝试使用wininet中的其他功能,我会不断遇到错误函数,如:

AttributeError: function 'InternetCheckConnection' not found

这几乎适用于wininet的任何其他功能,但这并不让我感到惊讶,因为dir(wininet)中唯一命名的函数是InternetGetConnectedState。

wininet方法显然可行,但我不知道如何继续使用它[特别是考虑到我只在工作中使用Windows]。

2 个答案:

答案 0 :(得分:0)

“好的,措辞太差了 - 让我们改为:打开网页连接并使用python获取其内容”

听起来您确实需要BeautifulSoupRequests。以下是用于浏览网页的快速example

答案 1 :(得分:0)

首先,我强烈建议安装requests模块。在Python上没有它做HTTP非常痛苦。

根据this answer,您需要从主机wpad.dat下载wpad。这是一个包含代理地址的文本文件。

了解代理设置后,您可以配置requests以使用它们:

import requests

proxies = {
  'http': 'http://10.10.1.10:3128',
  'https': 'http://10.10.1.10:1080',
}

requests.get('http://example.org', proxies=proxies)