如何使用 Python 访问网络?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3302427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:20:55  来源:igfitidea点击:

How to access the web with Python?

python

提问by user216171

I want to access websites without using their API. Would i do this by using something like Mechanize?

我想在不使用他们的 API 的情况下访问网站。我会通过使用 Mechanize 之类的东西来做到这一点吗?

采纳答案by dwich

回答by Cheery

You can access websites by HTTP protocol client: httplib

可以通过HTTP协议客户端访问网站:httplib

Though maybe you'd like urllib2, in specific the urllib2.urlopen

虽然也许你喜欢urllib2,特别是 urllib2.urlopen

Here's little example about using urllib2:

这是关于使用 urllib2 的小例子:

import urllib2
page = urllib2.urlopen("http://example.com/").read()
print page

回答by John Howard

Use urllib.requestin python 3.x, and urllib2 in python 2.x

在 python 3.x 中使用urllib.request,在 python 2.x 中使用urllib2

回答by Aniket

#for Python 3.2
import urllib.request
page = urllib.request.urlopen("http://www.google.com")
print (page.read())