BeautifulSoup4.4 python 2.7 抓包 qq news(代码目前没有跑起来???)

简介:   BeautifulSoup 下载 https://www.crummy.com/software/BeautifulSoup/bs4/download/4.1/ 本人的python 安装在I:\software\Python27\beautifulsoup4-4.

 


BeautifulSoup 下载
https://www.crummy.com/software/BeautifulSoup/bs4/download/4.1/

本人的python 安装在I:\software\Python27\beautifulsoup4-4.1.3
按住ctrl 右键 在此处打开命令行
dir  存在 setup.py

 

setup.py bulid

setup.py install

 

 

验证如下: 就没有问题

 

 

# -*- coding: UTF-8 -*-  
'''
Created on 2016年8月1日

@author: cmcc-B100036
'''
 
#http://ssdfz001.iteye.com/blog/2228685


import  urllib2,os,codecs  
from bs4 import BeautifulSoup  
#跟网址 http://news.qq.com/c/816guonei_1.htm
url='http://news.qq.com/c/816guonei_1.htm'
#存储路径
save_path='I:/software/Python27/pythonData/'
save_img='text.text'
save_txt='png.png'
#抽取正则
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):
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
    urllib2.install_opener(opener) 
    req = urllib2.Request(url)
    opener.addheaders = heads.items()
    respHtml = opener.open(req).read()
    return respHtml;

#获取新闻列表
def getList(url):
    contents=[]
    respHtml = getHtml(url)
    soup = BeautifulSoup(respHtml,from_encoding="gb2312")
    list = soup.find_all('div','class_=Q-tpList')
    for x in list:
        contents.append(x)
    return contents
#获取文本信息到本地
def loadText(contents):
    for content in contents :
        load(content)
#下载资源
def load(content): 
    soup = BeautifulSoup(content,from_encoding="gb2312")
    newsdetailname=soup.find_all('a','class_=pic').attrs["href"].get_text().replace('.htm','')
    newsimagpichref= soup.find_all('img','class_=picto').attrs["src"].get_text() 
    newstitle = soup.find_all('a','class_=linkto').get_text() 
    newscontent = soup.find_all('p','class_=112').get_text() 
    save_path  += newsdetailname; 
    if not os.path.exists(save_path):
        os.mkdir(save_path) 
    newstext = save_path+'\%s'%save_txt
    newsimg= save_path+'\%s'%save_img
    if not os.path.exists(newstext):
        os.mkdir(newstext)
    if not os.path.exists(newsimg):
        os.mkdir(newsimg) 
    imgsrc= urllib2.urlopen(newsimagpichref).read() 
    with  codecs.open(newsimg,"a+", "gb2312") as fp:
        fp.write(imgsrc)
    with codecs.open(newstext,'r','gb2312') as fp:
        fp.write(newsimagpichref+'\t'+newstitle+'\t'+newscontent+'\t')  
    print '------------------------------------------------------------ end one news' 
      
if __name__=="__main__":
#     url=raw_input("""输入目标网址\n       按回车键结束\n""")
     url='http://news.qq.com/c/816guonei_1.htm'
     contents = getList(url)
     loadText(contents)

 

 

 

 

package com.curiousby.python.demo;

import org.python.util.PythonInterpreter;

/**
 * @author baoyou E-mail:curiousby@163.com
 * @version 2016年8月1日 下午1:05:36 
 * desc: ...
 */
public class PythonByJava2 {

	
	public static void main(String[] args) {
		PythonInterpreter interpreter = new PythonInterpreter();  
        interpreter.execfile("I:\\cache\\ea-ws\\DemoJava\\conf\\newsqq.py");   
	}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

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



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

 

 

 

 

 

目录
相关文章
|
6天前
|
Python
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
|
1天前
|
数据安全/隐私保护 Python
探索Python中的装饰器:简化代码,提升效率
【9月更文挑战第32天】在Python编程世界中,装饰器是一个强大的工具,它允许我们在不改变函数源代码的情况下增加函数的功能。本文将通过直观的例子和代码片段,引导你理解装饰器的概念、使用方法及其背后的魔法,旨在帮助你写出更加优雅且高效的代码。
|
4天前
|
机器学习/深度学习 搜索推荐 数据可视化
Python量化炒股常用的Matplotlib包
Python量化炒股常用的Matplotlib包
19 7
|
4天前
|
数据采集 数据可视化 数据挖掘
Python量化炒股常用的Pandas包
Python量化炒股常用的Pandas包
19 7
|
4天前
|
人工智能 算法 数据处理
Python常用的Numpy包
Python常用的Numpy包
17 7
|
4天前
|
人工智能 数据可视化 搜索推荐
Python异常模块与包
Python异常模块与包
|
5天前
|
Python
? Python 装饰器入门:让代码更灵活和可维护
? Python 装饰器入门:让代码更灵活和可维护
11 4
|
5天前
|
缓存 测试技术 Python
探索Python中的装饰器:简化代码,提高可读性
【9月更文挑战第28天】在Python编程中,装饰器是一个强大的工具,它允许我们在不修改原有函数代码的情况下增加额外的功能。本文将深入探讨装饰器的概念、使用方法及其在实际项目中的应用,帮助读者理解并运用装饰器来优化和提升代码的效率与可读性。通过具体示例,我们将展示如何创建自定义装饰器以及如何利用它们简化日常的编程任务。
10 3
|
3天前
|
机器学习/深度学习 数据格式 Python
将特征向量转化为Python代码
将特征向量转化为Python代码
10 1
|
4天前
|
开发者 Python
30天拿下Python之模块和包
30天拿下Python之模块和包
下一篇
无影云桌面