随笔分类 - Python
摘要:1. 自动任务的功能为: 定时扫描数据库中的记录,然后发邮件 代码如下 scheduleMail.py 2. 把它做成后台任务的shell脚本如下 scheduleMail.sh 3. 如何杀死后台任务 这里有个坑,很多网上的博客没有说,我在这里提一下,以免大家重复去踩。 杀死该任务,就像杀死传统L
阅读全文
摘要:有的时候,我们本来写得好好的爬虫代码,之前还运行得Ok, 一下子突然报错了。 报错信息如下: Http 800 Internal internet error 这是因为你的对象网站设置了反爬虫程序,如果用现有的爬虫代码,会被拒绝。 之前正常的爬虫代码如下: 这个时候,需要我们给我们的爬虫代码做下伪装
阅读全文
摘要:我们在平时的工作中经常会遇到这样的需求,需要再某个时间点执行一段程序逻辑。 那么,在python中我们是怎么做的呢? 下面看代码: waitDesignatedTimeToRun.py 运行该小脚本,结果如下: Program not starting yet... (这里程序等待到制定时间)Pro
阅读全文
摘要:import urllib.request def download(url, num_retries=2): print('Downloading:', url) try: html = urllib.request.urlopen(url).read() except urllib.URLError as e: print('Down...
阅读全文
摘要:1. 下载PyMysql并且安装 下载地址 下载zip包后解压到目录,进入该目录,执行以下命令安装 python setup.py install 2. 编写一个简单的数据库访问程序 simple_mysql.py 3. 执行脚本 python simple_mysql.py
阅读全文
摘要:运行结果: b'HTTP/1.1 200 OK\r\nDate: Sun, 14 Feb 2016 03:34:20 GMT\r\nContent-Type: text/html\r\nContent-Length: 14613\r\nLast-Modified: Wed, 03 Sep 2014
阅读全文
摘要:例子文件如下: 一些复杂的读取操作getCells.py 运行结果: Apples1 Apples3 Pears5 Apples7 Strawberries A1 2015-04-05 13:34:02B1 ApplesC1 73***********************************
阅读全文
摘要:首先,需要安装openpyxl库 http://openpyxl.readthedocs.org/en/default/ pyton 2.xpip install openpyxl python 3.x easyinstall openpyxl 准备测试excel文件 firstExcel.py 运
阅读全文
摘要:下载博客园的logofrom urllib.request import urlretrievefrom urllib.request import urlopenfrom bs4 import BeautifulSouphtml = urlopen("http://www.cnblogs.com"...
阅读全文
摘要:运行结果: [{'number': 0}, {'number': 1}, {'number': 2}]{'number': 1}3pear
阅读全文
摘要:我们用到了requests库,由于是第三方的,必须下载 如果是python 2.x用下面命令 pip install requests python 3.x用下面命令 easy_install requests 运行结果: Searching for requestsReading https://
阅读全文
摘要:Negative Indexes(负索引) Getting Sublists with Slices • spam[2] is a list with an index (one integer).• spam[1:4] is a list with a slice (two integers).
阅读全文
摘要:1. 简单的if/else条件判断 judge_flow.py 运行结果: Please input name: davidInvalid user! Please input name: masterHello MasterPlease input password: aaaWrong passw
阅读全文
摘要:给程序加上控制台菜单menu.pyimport sysfrom notebook import Notebook, Noteclass Menu: '''Display a menu and respond to choices when run.''' def __init__(sel...
阅读全文
摘要:notebook.pyimport datetimelast_id = 0class Note: '''Represent a note in the notebook. Match against a string in searches and store tags for each...
阅读全文
摘要:下面我们创建一个真正的爬虫例子爬取我的博客园个人主页首页的推荐文章列表和地址scrape_home_articles.pyfrom urllib.request import urlopenfrom bs4 import BeautifulSoupimport rehtml = urlopen("h...
阅读全文
摘要:符号描述 例子(表达式)例子(匹配的字符串) *Matches the preceding character, subexpression, or bracketed character,0 or more times匹配0次或多次a*b*aaaaaaaa,aaabbbbb, bbbbbb ...
阅读全文
摘要:handle_excpetion.pyfrom urllib.request import urlopenfrom urllib.error import HTTPErrorfrom bs4 import BeautifulSoupimport sysdef getLogo(url): try...
阅读全文
摘要:目标:我们解析百度首页的logobs_baidu_logo.pyfrom urllib.request import urlopenfrom bs4 import BeautifulSouphtml = urlopen("http://www.baidu.com")bsObj = Beautiful...
阅读全文
摘要:1. 安装Beautiful Soup下载地址http://www.crummy.com/software/BeautifulSoup/bs4/download/4.4/解压后,进入根目录控制台下运行:python setup.py install运行结果:Processing dependenci...
阅读全文