
我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
# coding=utf-8 import HTMLParser import urllib import sys import re import os # 定义HTML解析器 class parseLinks(HTMLParser.HTMLParser): # 该方法用来处理开始标签的,eg:<div id="main"> def handle_starttag(self, tag, attrs): def _attr(attrlist, attrname): for each in attrlist: if attrname == each[0]: return each[1] return None if tag == 'a' or tag == "li" or tag == "link": # 如果为<a>标签 # name为标签的属性名,如href、name、id、onClick等等 for name, value in attrs: if name == 'href': # 这时选择href属性 #print "name_value: ", value # href属性的值 link_file.write(value) link_file.write("\n") #print "title: ", _attr(attrs, 'title') #print "first tag:", self.get_starttag_text() # <a>标签的开始tag #print "\n" def search_info(link, key): name = key text = urllib.urlopen(link).read() file_object = open("text.txt", "w") file_object.write(text) file_object.close() file_read = open("text.txt", "r") for line in file_read: if re.search(name, line): print line file_result.write(line) file_result.write("\n") file_read.close() def deep_search(link, depth): lParser.feed(urllib.urlopen(link).read()) if __name__ == "__main__": #处理输入 website = raw_input("请输入需要搜索的网站(exp:http://www.baidu.com): ") key = raw_input("请输入需要搜索的关键字: ") print "需要查找的网站是:", website print "我知道了主人,您需要找关键字:", key # 创建HTML解析器的实例 lParser = parseLinks() # 深度搜索子链接 link_file = open("sub_link.txt", "w") deep_search("http://www.baidu.com", 10) link_file.close() # 查找子链接中的信息 sub_link = open("sub_link.txt", "r") file_result = open("result.txt", "w") for sublink in sub_link: #print sublink if re.search("http", sublink): search_info(sublink, key) file_result.close() sub_link.close() lParser.close()
如果函数无参数,那么应声明其参数为void <pre name="code" class="cpp"> int function() { return 1; } int main(void) { int ret = 0; ret = function(2); printf("rst:%d\n", ret); return 0; } 如果用gcc编译器(即C语言编译器)编译如上代码,没有编译错误,函数正常return(1),但是此时程序员已经误用了function函数,给其传入了参数,但是编译器忽略了,因为在定义function函数的时候没有指定参数必须为空,而是处于缺省状态。 但是如果使用g++(C++编译器)编译器去编译这段代码,就会出现编译错误如下: "error: too many arguments to function ''int function()" 所以,无论在C 还是C++中,若函数不接受任何参数,一定要指明参数为void。以免某些编译器忽略了代码错误!!! 参考: http://c.biancheng.net/cpp/html/444.html
extern 关键字放在函数声明之前: > test.h extern int test(); 如果这样函数的声明中带有关键字extern,仅仅是暗示这个函数可能在别的源文件里定义。 这样一来,就是在程序中取代include “*.h”来声明函数,在一些复杂的项目中,比较习惯在所有的函数声明前添加extern修饰,以防止遗漏包含头文件而导致的编译错误。 extern 关键字放在函数定义之前: > test.c extern int test() { return true; } 如果在函数定义的地方带有关键字extern,表示该函数会提供给外部文件使用,其实有些编译器是默认每个函数都是extern类型的,反之是static类型 参考: http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777431.html