python3.5 beautiful4.4 扣扣国内新闻 爬虫
2016-11-19 17:22
483 查看
Java代码
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
'''
Created on 2016年11月18日
@author: baoyou <span style="line-height: 1.5;">curiousby@163.com</span>
'''
下载
#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---------------------------------------')
相关文章推荐
- python2.7 爬虫初体验爬取新浪国内新闻_20161130
- python 3.5:爬虫--爬取人民日报1946-2003年所有新闻
- python3.5——爬虫实现批量下载图片(正则表达式法和BeautifulSoup第三方库法)
- 基于Scrapy框架的Python新闻爬虫
- python3.5爬虫实例:根据城市名称来获取该城市最近七天的天气预报
- Python爬虫项目,获取所有网站上的新闻,并保存到数据库中,解析html网页等(未完待续)
- python3.5 爬虫 基于广度优先算法
- python爬虫——获取新浪新闻前两页新闻信息
- 第六课 Python新浪新闻爬虫最终整理总结
- python-爬虫(今日新闻头条练手)
- Python基于urllib,re爬取百度的国内即时新闻
- 19Python爬虫--爬取新浪新闻标题并保存到数据库
- python3+urllib撸新浪滚动新闻爬虫
- Python3.5 爬虫之由浅入深(三、html转excel)
- python爬虫爬取合肥工业大学校园新闻
- python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容
- python 爬取新浪国内新闻含获取js里的数据
- python 3.5 写的爬虫
- Python爬虫爬取新浪新闻内容
- 关于python网络爬虫——摘取新闻标题及链接