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



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

 

 

 

 

 

目录
相关文章
|
5天前
|
并行计算 C语言 开发者
优化Python代码的五大技巧
Python作为一种流行的编程语言,在各种应用场景中广泛使用。然而,随着项目规模的增长和需求的变化,Python代码的性能和可维护性也成为了关键问题。本文将介绍优化Python代码的五大技巧,帮助开发者提升代码效率和质量。
|
15天前
|
监控 Python
Python中的装饰器:提升代码灵活性与可读性
在Python编程中,装饰器是一种强大的工具,能够提升代码的灵活性和可读性。本文将介绍装饰器的基本概念、使用方法以及实际应用场景,帮助读者更好地理解和利用这一功能。
|
17天前
|
人工智能 数据可视化 数据挖掘
【python】Python航空公司客户价值数据分析(代码+论文)【独一无二】
【python】Python航空公司客户价值数据分析(代码+论文)【独一无二】
|
22天前
|
数据采集 JSON 数据可视化
【python】python懂车帝数据可视化(代码+报告)
【python】python懂车帝数据可视化(代码+报告)
|
21天前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
|
16天前
|
缓存 监控 算法
优化Python代码性能的10个技巧
提高Python代码性能是每个开发者都需要关注的重要问题。本文将介绍10个实用的技巧,帮助你优化Python代码,提升程序的运行效率和性能表现。无论是避免内存泄漏、减少函数调用次数,还是使用适当的数据结构,都能在不同场景下发挥作用,使你的Python应用更加高效稳定。
|
1天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
18 0
|
1天前
|
人工智能 Python
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
【AI大模型应用开发】【LangChain系列】实战案例1:用LangChain写Python代码并执行来生成答案
5 0
|
1天前
|
Linux 网络安全 开发工具
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
10 0
|
2天前
|
数据安全/隐私保护 Python
Python中的装饰器:提升代码可读性和灵活性
Python中的装饰器是一种强大的编程工具,能够提升代码的可读性和灵活性。本文将深入探讨装饰器的原理和用法,以及如何利用装饰器来简化代码、实现日志记录、权限控制等功能,从而让你的Python代码更加优雅和高效。

热门文章

最新文章