Skip to content

Instantly share code, notes, and snippets.

View staticor's full-sized avatar

SteveYagn staticor

View GitHub Profile
@staticor
staticor / key-binding. sublime text
Created August 15, 2013 03:51
key binding in sublime text
[
{ "keys": ["alt+m"], "command": "markdown_preview", "args": {"target": "browser"} },
{ "keys": ["enter"],
"command": "move",
"args": {"by": "characters", "forward": true},
"context":
[
{ "key": "following_text", "operator": "regex_contains", "operand": "^[)\\]\\>\\'\\\"]", "match_all": true },
]
@staticor
staticor / python.sublime-snippet
Created August 15, 2013 03:40
python snippet in sublime text 2 or 3
<snippet>
<content><![CDATA[
#!/usr/bin/env python
#-*- coding:utf -8-*-
'''
#Author: Staticor
#Company: EverString
#File:
#Description: this program
#Revision: //2013
@staticor
staticor / countdown_days.rb
Created August 18, 2013 14:21
ruby_ count down special days __ to come
#!/usr/bin/ruby
# Calculate the countdown for the special day.
require 'Date'
days = (DateTime.new(2013,10,20) - DateTime.now).ceil
puts "Maybe #{days} days left."
@Test
public void testDemo() {
JiebaSegmenter segmenter = new JiebaSegmenter();
String[] sentences =
new String[] {"这是一个伸手不见五指的黑夜。我叫孙悟空,我爱北京,我爱Python和C++。", "我不喜欢日本和服。", "雷猴回归人间。",
"工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作", "结果婚的和尚未结过婚的"};
for (String sentence : sentences) {
System.out.println(segmenter.process(sentence, SegMode.INDEX).toString());
}
}
@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
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 
 
>>> 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>'