安装和入门案例 腾讯云主机Python3环境安装Scrapy爬虫框架过程及常见错误 scrapy 爬虫框架入门案例详解 常见问题 No module named _util pip install Twisted==16.4.1 参考文档 Scrapy连接到各类数据库(SQLite,Mysql,Mongodb,Redis)
python学习笔记
标签: python学习笔记
Python : UnicodeEncodeError: 'latin-1' codec can't encode character
>>> u = u'hello\u2013world' # 方案一 >>> u.encode('latin-1', 'replace') # replace it with a question mark 'hello?world' # 方案二 >>> u.encode('latin-1', 'ignore') # ignore it 'helloworld' 或者自己替换 >>> u.replace(u'\u2013', '-').encode('latin-1') 'hello-world' 如果需要输出utf-8 >>> u.
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学习笔记:判断和循环的实现
author = 'pengwei' #py的判断语句 a, b, c = 1, 2, 3 if a > b: print(1) else: print(0) #python中是没有swith语句的 if a > b: print(1) elif a > c: print(2) else: print(3) c = a if a > b else b c = [b, a][a > b] c = (a > b and [a] or [b]
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') 运行程序,浏览器自动打开一个抓取的目标网站内容的网页。