Skip to content

Instantly share code, notes, and snippets.

View staticor's full-sized avatar

SteveYagn staticor

View GitHub Profile
@staticor
staticor / write_a_csv.py
Created August 18, 2013 23:30
write csv files
# _*_ coding:utf-8 _*_
import csv
csvfile = file('csvtest.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['id', 'url', 'keywords'])
data = [
@staticor
staticor / get_article_from_html.py
Created August 19, 2013 02:41
get_article_from_html.py
# -*- coding=utf-8 -*-
import os
import sys
import re
import string
import cProfile
import urllib2
reload(sys) ;sys.setdefaultencoding('utf8')
单引号、双引号和三双引号的区别


先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行

如:

s1 = "hello,world"

如果要写成多行,那么就要使用\ (“连行符”)吧,如

s2 = "hello,\

world"

s2与s1是一样的。如果你用3个双引号的话,就可以直接写了,如下:

s3 = """hello,

world,

hahaha.""",那么s3实际上就是"hello,\nworld,\nhahaha.", 注意“\n”,所以,

如果你的字符串里\n很多,你又不想在字符串中用\n的话,那么就可以使用3个双

引号。而且使用3个双引号还可以在字符串中增加注释,如下:

s3 = """hello,  #hoho, this is hello, 在3个双引号的字符串内可以有注释哦

world,          #hoho, this is world

hahaha."""

这就是3个双引号和1个双引号表示字符串的区别了,3个双引号与1个单引号的区别也
是和这个一样的,实际上python支持单引号是有原因的,下面我来比较1个单引号和

1个双引号的区别。


当我用单引号来表示一个字符串时,如果要表示 Let's go 这个字符串,必须这样:

s4 = 'Let\'s go',注意没有,字符串中有一个',而字符串又是用'来表示,所以

这个时候就要使用转义符 \ (\,转义符应该知道吧), 如果你的字符串中有一大堆

的转义符,看起来肯定不舒服,python也很好的解决了这个问题,如下:

s5 = "Let's go"

这时,我们看,python知道你是用 " 来表示字符串,所以python就把字符串中的那

个单引号 ' , 当成普通的字符处理了,是不是很简单。

对于双引号,也是一样的,下面举个例子

s6 = 'I realy like "python"!'

这就是单引号和双引号都可以表示字符串的原因了。
分类: Python
@staticor
staticor / get_real_site_from_baidu
Created August 19, 2013 03:49
抓取百度真实地址.
# _*_ coding:utf-8 _*_
#xiaohei.python.seo.call.me:)
#win+python2.7.x
import requests
for i in open('url.txt'):
r = requests.get(i.rstrip())
print r.url
>>> import re
>>> s = '''<table>
<tr>
<td>序列号</td><td>DEIN3-39CD3-2093J3</td>
<td>日期</td><td>2013年1月22日</td>
<td>售价</td><td>392.70 元</td>
<td>说明</td><td>仅限5用户使用</td>
</tr>
</table>'''
>>> res = r'<td>(.*?)</td><td>(.*?)</td>'
from bs4 import BeautifulSoup
import urllib2
import re
html=urllib2.urlopen("http://www.baidu.com/s?wd=seo&rn=100").read()
soup=BeautifulSoup(html)
results = soup.find_all(class_=re.compile("result"))
for links in results:        
         print links.h3.get_text()
         print links.span.get_text()
 
按下f5 得到结果
seo_百度百科
SEO综合查询 - 站长工具
 seo.chinaz.com/ 2012-11-14  
搜外SEO论坛-人气最旺的SEO行业社区,时刻交流搜索引擎技术更新...
  www.seowhy.com/bbs/ 2012-11-14  
SEO每天一贴 – Zac的搜索引擎优化博客。嗯,这个才是官方网站。
  www.seozac.com/ 2012-11-2  
百度SEO优化_外链及关键词SEO工具_百度站长平台
百度搜索引擎优化指南 Baidu SEO Guide 2.0 - seo培训入门必读
  baiduseoguide.com/ 2012-10-31  
seo_百度词典
SEO Company | Search Engine Optimization Firm - GreenCowSEO.com
  www.greencowseo.com/ 2012-11-14  
合肥优派&UPAI SEO/SEM优化中小企业营销利器.
  www.upai.net.cn/ 2012-10-15 
 
rom BeautifulSoup import BeautifulSoup
pageSource='''...omitted for brevity...'''
soup = BeautifulSoup(pageSource)
alltables = soup.findAll( "table", {"border":"2", "width":"100%"} )
results=[]
for table in alltables:
rows = table.findAll('tr')
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from pymongo import *
import os
import pprint
import sys
#sys.setdefaultencoding('utf8')
#from pymongo.connection import Connection
(prefer-coding-system ‘utf-8)
(setq
el-get-sources
'((:name asciidoc
:type elpa
:after (progn
(autoload 'doc-mode "doc-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.adoc$" . doc-mode))
#coding:utf-8
'''
    模拟登陆163邮箱并下载邮件内容
    @author: fc_lamp
    @blog:fc-lamp.blog.163.com
'''
import urllib
import urllib2
import cookielib
import re