python学习笔记

标签: python学习笔记

py学习笔记:抓取腾讯新闻

抓取的网址为:http://news.qq.com/china_index.shtml coding=utf-8 # author = 'pengwei' import urllib import re def main(): url = "http://news.qq.com/china_index.shtml" f = urllib.urlopen(url) html = f.read().decode('gbk') #print(html) pattern = re.compile(r"(.*)") for m in pattern.finditer(html): #print(m.group(

阅读全文...

py学习笔记:正则表达式的使用

author = 'pengwei' # encoding: UTF-8 import re pattern = re.compile(r"d+") match = pattern.match('hello world!hello1212') if match: print(match.group()) else: print("不匹配") match = pattern.search('hello12 world!43') if match: print(match.group()) else: print("未找到") #分割字符串 print(pattern.split('one1two2three3four44a')) #匹配全部 print(pattern.findall('one1two2three3four467')) #搜索string,

阅读全文...

py学习笔记:python基础学习

面向对象基础 # __author__ = 'pengwei' from abc import ABCMeta, abstractmethod # 子类1 class Child1: def __init__(self): pass def print_string(self): print("class child1") # 子类2 class Child2: def __init__(self): pass def print_string(self): print("class child2") # 多重继承 class Parent(Child2, Child1): # 类属性 name = 'xxxx' def __init__(self): Child2.__init__(self)

阅读全文...

py学习笔记:python定义类

coding=utf-8 #author = 'pengwei' class People: name = 'jack' # public age = 12 # private def printName(self): print(self.name) def getAge(self): return self.age #构造函数 def init(self, name, age): self.name = name self.age = age #析构函数 def __del(self): self.name = None p = People('pw', 12) p.printName() 定义成员变量时,

阅读全文...

py学习笔记:python3.4连接mysql数据库

下载第三方mysql库 http://yunpan.cn/QC2gemBIcUvui  访问密码 07cf 解压到某个文件夹下,通过dos进入文件夹,并输入 python setup.py install进行安装 coding=utf-8 #author = 'pengwei' #导入mysql库 import pymysql #连接数据库 db = pymysql.connect(host="localhost", user="root", passwd="wei8888go", db="score", charset='utf8') cursor = db.cursor() cursor.execute("SELECT * FROM users") result = cursor.fetchall() for record in result:

阅读全文...

py学习笔记:第三方库引入&抓取网页内容

下载第三方库httplib2 http://yunpan.cn/QCvB7ZtiYJerd  访问密码 8420 配置python的环境变量 在path中加入python的安装目录 执行第三方库中的setup.py文件完成安装 coding=utf-8 #author = 'pengwei' import urllib.request import webbrowser url = "http://pw.ecjtu.org/" response = urllib.request.urlopen(url) page = response.read() #print(page) open('pw.html', 'wb').write(page) webbrowser.open_new('pw.html') 运行程序,浏览器自动打开一个抓取的目标网站内容的网页。

阅读全文...