Python实现简单的爬虫获取某刀网的更新数据

昨天晚上无聊时,想着练习一下Python所以写了一个小爬虫获取小刀娱乐网里的更新数据

[python] view plain copy
 
  1. #!/usr/bin/python  
  2. # coding: utf-8  
  3.   
  4. import urllib.request  
  5. import re  
  6. #定义一个获取网页源码的子程序  
  7. head = "www.xiaodao.la"  
  8. def get():  
  9.     data = urllib.request.urlopen('http://www.xiaodao.la').read()  
  10.     #解码并去除无用文字  
  11.     str = data.decode("gbk").replace(r"font-weight:bold;","").replace(r" ","").replace(" ","").replace(" ","").replace(" ","").replace("#FF0000","#000000").strip()  
  12.     return str[str.find("好卡售"):str.find("20160303184868786878.gif")]#返回指定内容  
  13. #获取一次网页源码并赋值给str  
  14. str = get();  
  15. #print(str)  
  16.   
  17. #定义正则表达式  
  18. #reg = r'href="(.*?)"style="color:#000000;"title="(.*?)"target="_blank">'  
  19. reg = r'href="(.*?)"style="color:#000000;"title="(.*?)"target="_blank">(.*?)</a></div></td><tdwidth=12.5%align=rightnowrap=nowrapstyle="color:#F00;">(.*?)</td>'  
  20.   
  21. tmp = re.compile(reg);#创建正则表达式  
  22. list = re.findall(tmp,str);#正则表达式匹配  
  23. list = tuple(list)#转换类型  
  24.   
  25. print("一共匹配到%d个"%(len(list)))#输出匹配数量  
  26. #print(list)  
  27.   
  28. for i in range(len(list)):  
  29.     print("当前第%d个:"%(i+1))  
  30.     print("标题:%s 地址:%s更新时间:%s "%(list[i][1],head + list[i][0],list[i][3]))  
原文地址:https://www.cnblogs.com/zxtceq/p/8985732.html