pythonopenurl
‘壹’ 如何用python的urlopen打开自动跳转的网页
try this:
import sys,re,urllib2,cookielib
def download(url):
____opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
____opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)')]
____f = opener.open(url)
____s = f.read()
____f.close()
____return s
s = download("http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2009-2403")
print s
‘贰’ python openurl函数会url编码吗
面是中文的情况(比如‘丽江'),url的地址编码却是'%E4%B8%BD%E6%B1%9F',因此需 要做一个转换。这里我们就用到了模块urllib。
?
1
2
3
4
5
6
7
8
>>> import urllib
>>> data = '丽江'
>>> print data
丽江
>>> data
'\xe4\xb8\xbd\xe6\xb1\x9f'
‘叁’ 如何使用python打开IE并打开一个URL
要查一下iexplore的命令行参数。另外比较好的是使用webbrowser模块,它是使用缺省浏览器,可能更方便,可以直接控制是否弹出新窗口。open(
url[,
new=0][,
autoraise=1])
Display
url
using
the
default
browser.
If
new
is
true,
a
new
browser
window
is
opened
if
possible.
If
autoraise
is
true,
the
window
is
raised
if
possible
(note
that
under
many
window
managers
this
will
occur
regardless
of
the
setting
of
this
variable).
open_new(
url)
Open
url
in
a
new
window
of
the
default
browser,
if
possible,
otherwise,
open
url
in
the
only
browser
window.
‘肆’ Python 正则匹配抓取到的网页超链接怎么URLopen
re.findall返回的是列表,遍历列表将其值做为参数传入urlopen即可,类似于下面这样
for url in re.findall(XXXXXX):
print urlopen(url).read()
‘伍’ python中。关于openurl的用法
用这个模块吧,
import webbrowser
‘陆’ 关于python urlopen函数
python
3里面,bytes存放的是binary
data,而str存放的text
从bytes转到str,需要把binary
data解码,因此你需要指定一个编码,例如:
my_str
=
str(my_bytes,
encoding="utf-8")
建议阅读文档:
http://docs.python.org/release/3.0.1/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit
http://docs.python.org/release/3.0.1/howto/unicode.html#unicode-howto
这两段文档应该足够解决你的困惑,呵呵
‘柒’ python程序问题:urlopen()控制
使用try...except来对urlopen出错进行控制,通过socket模块的setdefaulttimeout函数来控制超时时间,python3.x示例代码如下:
importurllib.request
importsocket
socket.setdefaulttimeout(10)#设置超时时间
#要下载的网页列表
urls=['http://www.test.com/1.htm',
'http://www.test.com/2.htm',
'http://www.test.com/3.htm',
'http://www.test.com/4.htm',
'http://www.test.com/5.htm',
'http://www.test.com/6.htm']
forurlinurls:
try:
html=urllib.request.urlopen(url)
#处理得到的网页
except:
#出错处理
‘捌’ python3 urlopen怎么用
urllib.request.urlopen 就是打开url网址的操作,参数可以是一个url,也可以是一个request对象,作用是一样的,上面的代码中 response就是上一步得到的request对象。
‘玖’ 为什么我的python urlopen连不上
这是网络错误的意思,与程序无关,可能是你的网络有问题,或者你要连接的网站与你本地网络的不兼容问题..
‘拾’ 谁能告诉我python中urlopen函数data参数的作用和意义
我认为,它指的是客户端向网络服务器发起url请求中,将被传递给服务接口程序处理所必需的一些参数,比如用户id、会话id、用户名、密码等等。