`
knight_black_bob
  • 浏览: 824643 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多

 

 



 

 

  

 

#!/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捐助),没钱捧个人场,谢谢各位。



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

 

 

 

 

  • 大小: 227.1 KB
  • 大小: 19.5 KB
1
0
分享到:
评论

相关推荐

    python3.5从零开始学电子书

    python3.5从零开始学是专门针对Python新手量身设计,涵盖了Python 3.5 实际开发的重要知识点。内容包括:Python语言的类型和对象、操作符和表达式、编程结构和控制流、函数、序列、多线程、正则表达式、面向对象编程...

    python3.5简化版教程

    python3.5简化版教程.python3.5简化版教程.python3.5简化版教程

    python3.5全栈工程师零基础到项目实战全套

    第一阶段:Python3.5基础语法篇 01.Python3.5介绍与循环 14课 02.Python3.5基本数据结构 14课 03.Python3.5函数与变量 19课 04.Python3.5装饰器变成 18课 05.Python3.5模块讲解14课 06.Python3.5面向对象12课 07....

    Python3.5高级拓展篇.zip

    1.Python3.5 openstack云计算专题 10课 Python3.5-01.云计算介绍.avi Python3.5-02.openstack组件介绍.avi Python3.5-03.keystone介绍.avi Python3.5-04.openstack组件通信流程.avi Python3.5-05.虚拟化介绍....

    Python3.5从零开始学.pdf

    新版本Python3.5基础教程,从零入门必备,新书

    python 3.5

    python.3.5.4可以直接安装使用,现在python很热门,欢迎大家下载学习。

    Python3.5基础语法+WEB开发+Django实战+高级拓展视频.zip

    01.Python3.5 介绍与循环 02.Python3.5 基本数据结构 03.Python3.5 函数与变量 04.Python3.5 装饰器编程 05.Python3.5 模块讲解 06.Python3.5 面向对象 07.Python3.5 网络编程 08.Python3.5 Socekt高级编程 09....

    Python3.5从零开始学PDF

    Python3.5从零开始学 PDF内容简介:《Python 3.5从零开始学》专门针对Python新手量身定做,涵盖了Python 3 实际开发的重要知识点,内容包括:Python语言的类型和对象、操作符和表达式、编程结构和控制流、函数、序列...

    Python3.5基础语法篇视频.zip

    02 Python3.5基本数据构14课rar 03 Python3.5函数与变量19果tar 04,Python3.5装编程18课rar 05,.Python3.5模块讲解14课rar 06,Python3.5面向对象12课rar 07.Python3.5网络编程13课rar 08,Python3.5 Socekti高级...

    Python 3.5

    2015年最新发布3.5版本,带帮助文件。

    适用于Python3.5版本的编译好的dlib文件

    适用于Python3.5版本的编译好的dlib文件,后缀是.whl 免去下载Cmake 直接install

    Python3.5爬虫项目班资料

    Python3.5爬虫项目班资料,爬虫的实现Python实现,需要的朋友下载看看。

    Win-Caffe:Visual Studio 2015, CPU only, Python 3.5

    caffe1.0 Windows release版本:Visual Studio 2015, CPU only, Python 3.5

    Python3.5从零开始学

    Python3.5从零开始学,专门针对Python新手量身定做,涵盖了Python 3 实际开发的重要知识点

    Python3.5Django实战篇视频.zip

    1.Python3.5 DjangoWeb开发框架 18课 2 Python3.5 Django的ORM映射机制20课.rar 3.Python3,5 Django数据库实战15课,rar 4,Python3.5 Django分页与Cookie19课.rar 5.Python3,5 Django会话与表单验证21课rar 6,Python...

    关于python3.5的机器学习、dlib、opencv-python、numpy等64位依赖包whl.zip

    大多数在官网下载比较慢,这些是whl文件,可直接安装dlib\numpy\opencv-python\scikit\sklearn

    《Python 3.5从零开始学》刘宇宙 mobi

    《Python 3.5从零开始学》专门针对Python新手量身定做,涵盖了Python 3 实际开发的重要知识点,内容包括:Python语言的类型和对象、操作符和表达式、编程结构和控制流、函数、序列、多线程、正则表达式、面向对象...

    python3.5可用的scipy

    scipy是python科学计算必不可少的包之一,而为python3.5准备的scipy比较少,特此奉上,(需要先安装python3.5、pip及Numpy)下载该文件后,使用pip install scipy-0.18.0rc1-cp35-cp35m-win_amd64.whl实现安装

    树莓派上Python3.5环境下Opencv的安装过程

    文档详细讲解了Opencv在树莓派Python3.5环境下的安装过程。

    Android平台的Python3.5.so

    重新编译的CrytaxNDK中的Python3.5so,可支持ssl,可在 Android的Armabi上运行的so

Global site tag (gtag.js) - Google Analytics