python3.5 beautiful4.4 扣扣国内新闻 爬虫

来源:互联网 发布:python 命令行输入 编辑:程序博客网 时间:2024/04/28 13:34


 

 

 

Java代码  收藏代码
  1. #!/usr/bin/python3  
  2. # -*- coding: UTF-8 -*-   
  3. '''  
  4. Created on 20161118日  
  5.   
  6. @author:  baoyou <span style="line-height: 1.5;">curiousby@163.com</span>  
  7. '''  
  8.   
  9.    
  10.      下载
  11. #http://ssdfz001.iteye.com/blog/2228685    
  12.      
  13. import  urllib.request  
  14. import urllib.parse  
  15. import os, sys   
  16. import codecs   
  17. import bs4   
  18. from bs4 import BeautifulSoup    
  19. import re  
  20. import urllib.request, urllib.parse, http.cookiejar   
  21.   
  22.        
  23. #跟网址 http://news.qq.com/c/816guonei_1.htm    
  24. base_url='http://news.qq.com/'  
  25. url='http://news.qq.com/c/816guonei_1.htm'    
  26. #存储路径    
  27. save_path='C:/Users/cmcc-B100036/Desktop/'    
  28. save_img='img'    
  29. save_txt='text'    
  30. #抽取正则    
  31. reg = '<a target=\"_blank\" class=\"pic\" href=\"([^\"]*)\"><img class=\"picto\" src=\"([^\"]*)\"></a><em class=\"f14 l24\"><a target=\"_blank\" class=\"linkto\" href=\"[^\"]*\">([^</a>]*)</a></em><p class=\"l22\">([^</p>]*)</p>'    
  32. #request消息头    
  33. heads = {     
  34. 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',    
  35. 'Accept-Encoding':'gzip, deflate, sdch',    
  36. 'Accept-Language':'zh-CN,zh;q=0.8',    
  37. 'Cache-Control':'max-age=0',    
  38. 'Host':'news.qq.com',    
  39. 'Proxy-Connection':'keep-alive',    
  40. 'Upgrade-Insecure-Requests':'1',    
  41. 'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'    
  42. }    
  43.     
  44. #获取网页信息    
  45. def getHtml(url):    
  46.     fp = urllib.request.urlopen(url)  
  47.     bytes = fp.read()  
  48.     respAllHtml = bytes.decode('gbk')  
  49.     fp.close();  
  50.     #print('---- respAllHtml----',respAllHtml);  
  51.     return respAllHtml;    
  52.     
  53. #获取新闻列表    
  54. def getList(url):     
  55.     respHtml = getHtml(url);  
  56.     #print('---- respHtml----',respHtml);  
  57.     soup = BeautifulSoup(respHtml ,'html.parser');    
  58.     list = soup.find_all('div',class_='Q-tpList');  
  59. #    print('-----------list .len------------',len(list));  
  60.     contents=[]     
  61.     for x in list:    
  62.         contents.append(x)    
  63.     return contents    
  64. #获取文本信息到本地    
  65. def loadText(contents):    
  66.     for content in contents :    
  67.         load(content)    
  68. #下载
    资源    
  69. def load(content):     
  70. #    print(content.prettify());    
  71. #      
  72. #    print(content.find('a',class_='pic'))  
  73. #    print(content.find('a',class_='pic')['href'])  
  74. #    print(content.find('a',class_='pic').img)  
  75. #    print(content.find('a',class_='pic').img['src'])  
  76. #    print( content.find('a',class_='linkto'))  
  77. #    print( content.find('a',class_='linkto').get_text())  
  78. #    print(content.find('p'))   
  79.     urlsuffix=content.find('a',class_='pic')['href'];  
  80.     detailurl=base_url + urlsuffix;   
  81.     detailimg= content.find('a',class_='pic').img['src'];     
  82.     detailtitle = content.find('a',class_='linkto').get_text();     
  83.     detailcontent = content.find('p').get_text();  
  84.       
  85.     save_path='C:/Users/cmcc-B100036/Desktop/'  
  86.     save_path   =  save_path+urlsuffix.replace(".htm","");    
  87.     if not os.path.exists(save_path):    
  88.         os.makedirs( save_path, 0o755 );     
  89.     newstext = save_path+'/%s'%save_txt    
  90.     newsimg= save_path+'/%s'%save_img    
  91.     if not os.path.exists(newstext):    
  92.         os.makedirs( newstext, 0o755 );    
  93.     if not os.path.exists(newsimg):    
  94.         os.makedirs( newsimg, 0o755 );       
  95.     urllib.request.urlretrieve(detailimg,newsimg+"/img.png"  );  
  96.     with codecs.open(newstext+"/text.txt",'w+','utf-8') as fp:    
  97.         fp.write(detailurl+'\t'+detailimg+'\t'+detailtitle+'\t'+detailcontent)      
  98.     #print ('------------------------------------------------------------ end one news')     
  99.           
  100. if __name__=="__main__":    
  101. #     url=raw_input("""输入目标网址\n       按回车键结束\n""")    
  102.     print ('---------------------start--------------------------------------')  
  103.     url='http://news.qq.com/c/816guonei_1.htm';   
  104.     contents = getList(url);  
  105.     loadText(contents);  
  106.     print ('---------------------end---------------------------------------')   

 

0 0
原创粉丝点击