使用python抓取360搜索联想词
几乎所有的搜索引擎都提供联想词提示,这个小脚本其实功能性不大,只当作使用urllib2发http请求和如何使用代理来访问
参考文章
几乎所有的搜索引擎都提供联想词提示,这个小脚本其实功能性不大,只当作使用urllib2发http请求和如何使用代理来访问
参考文章
相对于python解析XML来说,我还是比较喜欢json的格式返回,现在一般的api返回都会有json与XML格式的选择,json的解析起来个人觉得相对简单些
先看一个简单的豆瓣的图书查询的api返回
解析XML主要用到pytohn自带的XML库,其次还是lxml库
先以一个相对简单但功能比较全的XML文档为例
<?xml version='1.0' encoding='utf-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xml:lang='en'>
<title>dive into mark</title>
<subtitle>currently between addictions</subtitle>
<id>tag:diveintomark.org,2001-07-29:/</id>
<updated>2009-03-27T21:56:07Z</updated>
<link rel='alternate' type='text/html' href='http://diveintomark.org/'/>
<entry>
<author>
<name>Mark</name>
<uri>http://diveintomark.org/</uri>
</author>
<title>Dive into history, 2009 edition</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition'/>
<id>tag:diveintomark.org,2009-03-27:/archives/20090327172042</id>
<updated>2009-03-27T21:56:07Z</updated>
<published>2009-03-27T17:20:42Z</published>
<category scheme='http://diveintomark.org' term='diveintopython'/>
<category scheme='http://diveintomark.org' term='docbook'/>
<category scheme='http://diveintomark.org' term='html'/>
<summary type='html'>Putting an entire chapter on one page sounds
bloated, but consider this — my longest chapter so far
would be 75 printed pages, and it loads in under 5 seconds…
On dialup.</summary>
</entry>
<entry>
<author>
<name>Mark</name>
<uri>http://diveintomark.org/</uri>
</author>
<title>Accessibility is a harsh mistress</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2009/03/21/accessibility-is-a-harsh-mistress'/>
<id>tag:diveintomark.org,2009-03-21:/archives/20090321200928</id>
<updated>2009-03-22T01:05:37Z</updated>
<published>2009-03-21T20:09:28Z</published>
<category scheme='http://diveintomark.org' term='accessibility'/>
<summary type='html'>The accessibility orthodoxy does not permit people to
question the value of features that are rarely useful and rarely used.</summary>
</entry>
<entry>
<author>
<name>Mark</name>
</author>
<title>A gentle introduction to video encoding, part 1: container formats</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2008/12/18/give-part-1-container-formats'/>
<id>tag:diveintomark.org,2008-12-18:/archives/20081218155422</id>
<updated>2009-01-11T19:39:22Z</updated>
<published>2008-12-18T15:54:22Z</published>
<category scheme='http://diveintomark.org' term='asf'/>
<category scheme='http://diveintomark.org' term='avi'/>
<category scheme='http://diveintomark.org' term='encoding'/>
<category scheme='http://diveintomark.org' term='flv'/>
<category scheme='http://diveintomark.org' term='GIVE'/>
<category scheme='http://diveintomark.org' term='mp4'/>
<category scheme='http://diveintomark.org' term='ogg'/>
<category scheme='http://diveintomark.org' term='video'/>
<summary type='html'>These notes will eventually become part of a
tech talk on video encoding.</summary>
</entry>
</feed>
先简单的看一下这个XML的结构
最近无聊,想玩玩微信的公众平台,后来发现乐趣无穷啊~
使用的工具,python 新浪SAE平台,微信的公众平台
你需要先在微信的公众平台与新浪SAE平台上各种注册,微信平台注册的时候需要你拍张手持身份证的照片,还有几天的审核期
微信公众平台:http://mp.weixin.qq.com
利用python的smtplib,发邮件将是一件非常简单的事情,下文以用163邮箱来发邮件为例,说明smtplib的应用
#coding:utf-8
import smtplib
def sendMail(mail_to):
mail_server = 'smtp.163.com'
mail_port = '25'
username = 'soar_1987@163.com'
password = 'XXXXXXXX'
mail_title = 'python Test'
mail_content = 'This is a test from python for sending email'
if type(mail_to) == str:#之所以要有这样的判断是为了收件人是多个人或者传入的的收件人列表是以list的方式
mail_list = mail_to.split(';') #将str类型的数据转换为list
elif type(mail_to) == list:
mail_list = mail_to
else:
print "你输入的收件人格式有误"
try:
handle = smtplib.SMTP(mail_server,mail_port)
handle.login(username,password)
msg = "From:%srn To:%srnContent-Type: text/html;charset=gb2312rnSubject:%srnrn %s"%("杨彦星",";".join(mail_list),mail_title,mail_content) #这里的msg其实就是一种固定的格式,From:To:Subject
handle.sendmail(username,mail_list,msg)
print "Send email sucess"
except Exception,e:
print "Send email failed because %s" % e
if __name__ =="__main__":
sendMail('yanxingyang@gmail.com')
可爱的python一书中有一个练习题,是在一个目录中查找特定扩展名的文件,并且读取里面的内容,然后用户输入一个关键词,在这些文件中进行搜索,如果找到后就把这一行内容打印出来,他提出的问题是如果里面还有目录,目录里面还有更深的目录,解决这个问题,我想到的只能是递归。。。
#coding:utf-8
import os,sys
def cdGrep(keyword,filepath):
filelist = os.listdir(filepath)
for cdc in filelist:
if os.path.isdir(cdc):
filepath2 = os.path.join(filepath,cdc)
cdGrep(keyword,filepath2)
elif '.txt' in cdc:
# print filepath+cdc
f = open(filepath+''+ cdc)
for line in f.readlines():
linelower = line.lower()
if keyword in linelower:
print '您查找的关键词在%s中找到'% (filepath+''+cdc)
print line
f.close()
if __name__ == '__main__':
keword = raw_input('请输入想要查询的关键字')
pw = os.getcwd()
cdGrep(keword,pw)
小脚本很简单,主要是练习一下正则匹配,与简单的urllib库的应用
#coding: utf8
import urllib,re
import os
def getLastinstall():
page = urllib.urlopen(r'http://sd.360.cn/download_center.html')
html = page.read()
page.close()
rematch = r'http://down.360.cn/360sd/360sd_se_(.*?)exe'
replusmatch = r'http://down.360.cn/360sd/360sd_plus_(.*?)exe'
stdname = '360sd_'+(str(re.compile(rematch).findall(html)[0]))+'exe'
downurlstd = r'http://down.360.cn/360sd/360sd_'+(str(re.compile(rematch).findall(html)[0]))+'exe'
powname = '360sd_plus_'+(str(re.compile(replusmatch).findall(html)[0]))+'exe'
downurlpow = r'http://down.360.cn/360sd/360sd_plus_'+(str(re.compile(replusmatch).findall(html)[0]))+'exe'
localpath = os.getcwd()+r'/installpack'
if not (os.path.isfile(stdname) and os.path.isfile(powname)):
if not os.path.isdir('installpack'):
os.makedirs('installpack')
urllib.urlretrieve(downurlstd,localpath+'/'+stdname)
urllib.urlretrieve(downurlpow,localpath+'/'+powname)
else:
print '目录中已经是线上最新版杀毒安装程序'
if __name__ =='__main__':
getLastinstall()
原文地址:http://blog.csdn.net/qinyilang/article/details/5484415
**过量的参数
**在运行时知道一个函数有什么参数,通常是不可能的。另一个情况是一个函数能操作很多对象。更有甚者,调用自身的函数变成一种api提供给可用的应用。
对于这些情况,python提供了两种特别的方法来定义函数的参数,允许函数接受过量的参数,不用显式声明参数。这些“额外”的参数下一步再解释。
注意args和kwargs只是python的约定。任何函数参数,你可以自己喜欢的方式命名,但是最好和python标准的惯用法一致,以便你的代码,其他的程序员也能轻松读懂。 ** 位置参数**
在参数名之前使用一个星号,就是让函数接受任意多的位置参数。
>>> def multiply(*args):
... total = 1
... for arg in args:
... total *= arg
... return total
...
>>> multiply(2, 3)
6
>>> multiply(2, 3, 4, 5, 6)
720
早上匆匆忙忙的上了拥挤的公交,其实后面还有更拥挤的地铁,麻木的内心其实早已习惯这种乱哄哄的狭小空间,打开手机,习惯性的打开阅读软件,浏览着早已下载好的离线新闻,公交每到一站,任凭司机撕心裂肺的倡导,能动则随着人流动,不能动就当什么都没有听到,当有乘客在拥挤的车厢内挤来挤去,有意无意的一些身体接触摩擦过后,一股想骂人的冲动只因为在转头都困难的环境下就忍了吧。
怎知每天这时都会有很多老年人来凑热闹,我有时真是由衷的佩服他们,满头白发,却还做着有些年轻人都不想做但不得不做的事情,挤来挤去的,也有不开眼的在座位上的乘客,依然玩着自己的手机或者补着晚上还没有做完的梦,于是老年人在还可以移动的情况下会继续寻找属于自己的“专座”。
而今天我放下了手机,不再看那些用数据堆起来的专题。只因两位特别的老人。
一个小功能,我就是想用print功能实现,不想用write
import os
os.chdir("/usr/tem")
char="my name is yangyanxing"
f = open("test.txt","w")
print >>f,char
但是Python3中还可以用以下的方式
import os
os.chdir("/usr/tem")
char="my name is yangyanxing"
f = open("test.txt","w")
print(char,file=f)