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

简介:             #!/usr/bin/python3 # -*- coding: UTF-8 -*- ''' Created on 2016年11月18日 @author: baoyou curiousby@163.

 

 



 

 

  

 

#!/usr/bin/python3
# -*- coding: UTF-8 -*- 
'''
Created on 2016年11月18日

@author:  baoyou curiousby@163.com
'''

 
   
#http://ssdfz001.iteye.com/blog/2228685  
   
import  urllib.request
import urllib.parse
import os, sys 
import codecs 
import bs4 
from bs4 import BeautifulSoup  
import re
import urllib.request, urllib.parse, http.cookiejar 

     
#跟网址 http://news.qq.com/c/816guonei_1.htm  
base_url='http://news.qq.com/'
url='http://news.qq.com/c/816guonei_1.htm'  
#存储路径  
save_path='C:/Users/cmcc-B100036/Desktop/'  
save_img='img'  
save_txt='text'  
#抽取正则  
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>'  
#request消息头  
heads = {   
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',  
'Accept-Encoding':'gzip, deflate, sdch',  
'Accept-Language':'zh-CN,zh;q=0.8',  
'Cache-Control':'max-age=0',  
'Host':'news.qq.com',  
'Proxy-Connection':'keep-alive',  
'Upgrade-Insecure-Requests':'1',  
'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'  
}  
  
#获取网页信息  
def getHtml(url):  
    fp = urllib.request.urlopen(url)
    bytes = fp.read()
    respAllHtml = bytes.decode('gbk')
    fp.close();
    #print('---- respAllHtml----',respAllHtml);
    return respAllHtml;  
  
#获取新闻列表  
def getList(url):   
    respHtml = getHtml(url);
    #print('---- respHtml----',respHtml);
    soup = BeautifulSoup(respHtml ,'html.parser');  
    list = soup.find_all('div',class_='Q-tpList');
#    print('-----------list .len------------',len(list));
    contents=[]   
    for x in list:  
        contents.append(x)  
    return contents  
#获取文本信息到本地  
def loadText(contents):  
    for content in contents :  
        load(content)  
#下载资源  
def load(content):   
#    print(content.prettify());  
#    
#    print(content.find('a',class_='pic'))
#    print(content.find('a',class_='pic')['href'])
#    print(content.find('a',class_='pic').img)
#    print(content.find('a',class_='pic').img['src'])
#    print( content.find('a',class_='linkto'))
#    print( content.find('a',class_='linkto').get_text())
#    print(content.find('p')) 
    urlsuffix=content.find('a',class_='pic')['href'];
    detailurl=base_url + urlsuffix; 
    detailimg= content.find('a',class_='pic').img['src'];   
    detailtitle = content.find('a',class_='linkto').get_text();   
    detailcontent = content.find('p').get_text();
    
    save_path='C:/Users/cmcc-B100036/Desktop/'
    save_path   =  save_path+urlsuffix.replace(".htm","");  
    if not os.path.exists(save_path):  
        os.makedirs( save_path, 0o755 );   
    newstext = save_path+'/%s'%save_txt  
    newsimg= save_path+'/%s'%save_img  
    if not os.path.exists(newstext):  
        os.makedirs( newstext, 0o755 );  
    if not os.path.exists(newsimg):  
        os.makedirs( newsimg, 0o755 );     
    urllib.request.urlretrieve(detailimg,newsimg+"/img.png"  );
    with codecs.open(newstext+"/text.txt",'w+','utf-8') as fp:  
        fp.write(detailurl+'\t'+detailimg+'\t'+detailtitle+'\t'+detailcontent)    
    #print ('------------------------------------------------------------ end one news')   
        
if __name__=="__main__":  
#     url=raw_input("""输入目标网址\n       按回车键结束\n""")  
    print ('---------------------start--------------------------------------')
    url='http://news.qq.com/c/816guonei_1.htm'; 
    contents = getList(url);
    loadText(contents);
    print ('---------------------end---------------------------------------') 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

 

目录
相关文章
|
8天前
|
数据采集 Python
【python】爬虫-西安医学院-校长信箱
本文以西安医学院-校长信箱为基础来展示爬虫案例。来介绍python爬虫。
【python】爬虫-西安医学院-校长信箱
|
27天前
|
数据采集 Python
爬虫实战-Python爬取百度当天热搜内容
爬虫实战-Python爬取百度当天热搜内容
65 0
|
1月前
|
数据采集 存储 XML
深入浅出:基于Python的网络数据爬虫开发指南
【2月更文挑战第23天】 在数字时代,数据已成为新的石油。企业和个人都寻求通过各种手段获取互联网上的宝贵信息。本文将深入探讨网络爬虫的构建与优化,一种自动化工具,用于从网页上抓取并提取大量数据。我们将重点介绍Python语言中的相关库和技术,以及如何高效、合法地收集网络数据。文章不仅为初学者提供入门指导,也为有经验的开发者提供进阶技巧,确保读者能够在遵守网络伦理和法规的前提下,充分利用网络数据资源。
|
1月前
|
数据采集 测试技术 API
python爬虫之Appium 的使用
搭建appium环境,appium基本使用,API操作等等
41 0
|
1月前
|
数据采集 JSON 数据格式
python爬虫之app爬取-charles的使用
charles 基本原理,charles抓包,分析,重发。
46 0
|
1月前
|
数据采集 存储 NoSQL
Python爬虫Cookies 池的搭建
python爬虫Cookie池架构,实现
48 0
|
1月前
|
数据采集 NoSQL Redis
Python爬虫-代理池原理和搭建
代理池架构,代理池的实现
34 0
|
14天前
|
数据采集 安全 Python
python并发编程:Python实现生产者消费者爬虫
python并发编程:Python实现生产者消费者爬虫
22 0
python并发编程:Python实现生产者消费者爬虫
|
26天前
|
数据采集 数据挖掘 调度
异步爬虫实践攻略:利用Python Aiohttp框架实现高效数据抓取
本文介绍了如何使用Python的Aiohttp框架构建异步爬虫,以提升数据抓取效率。异步爬虫利用异步IO和协程技术,在等待响应时执行其他任务,提高效率。Aiohttp是一个高效的异步HTTP客户端/服务器框架,适合构建此类爬虫。文中还展示了如何通过代理访问HTTPS网页的示例代码,并以爬取微信公众号文章为例,说明了实际应用中的步骤。
|
1月前
|
数据采集 测试技术 API
python爬虫之app爬取-微信朋友圈
搭建appium环境,appium基本使用,API操作等等
66 0