Python网页抓取,,#coding:ut


#coding:utf-8import urllib   #导入模块print dir(urllib)   #查看urllib方法print help(urllib.urlopen)  #查看帮助文档url="http://www.baidu.com"  #定义网址html=urllib.urlopen(url)   #打开urlprint html.read()   #urlopen有一个方法是read()# 解决编码问题print html.read().decode("gb2312").encode("utf-8")# 忽略不能识别的内容print html.read().decode("gbk",‘ignore‘).encode("utf-8")  # 获取头部信息print html.info()# 获取状态码print html.getcode()# 获取url地址print html.geturl()# 下载网页urllib.urlretrieve(url,"F:\\1.txt")    # 参数:1、网址(必须是字符串);2、本地保存路径+文件名(注意Windows下的路径转义)    # 3、一个函数调用,可以任意定义函数的行为(要保证函数有3个参数)        # 3.1 到目前为止传递的数据块数量        # 3.2 每个数据块的大小,单位byte,字节        # 3.3 远程文件大小# 函数定义def callback(a,b,c):    """    这里是注释    """# 关闭打开的文件,这是很重要的!html.close()  # 判断内容code=html.getcode()    # 判断类型    print type(code)if code==200:    print "正常"else:    print "网页异常"

Python网页抓取

评论关闭