python爬新闻

import re
from urllib import request
from lxml import etree

testurl="http://news.163.com/rank/"

with request.urlopen(testurl) as f:
    print(‘Status:‘, f.status, f.reason)
    #网页的编码格式只取一次,默认所有的编码方式都是这个
    decode=(f.headers[‘Content-Type‘].split(‘;‘)[1]).split(‘=‘)[1]
    data = f.read().decode(decode.lower())
    infos = re.findall(r‘<div class="titleBar" id=".*?"><h2>(.*?)</h2><div class="more"><a href="(.*?)">.*?</a></div></div>‘, data, re.S)
    for i in range(len(infos)):
        print(‘%s-%s‘%(i,infos[i][0]))
    print(‘选择新闻类型‘)
    k=input()
    if k.isdigit()and int(k)<len(infos):
        newpage=(request.urlopen(infos[int(k)][1]).read()).decode(decode.lower())
        dom=etree.HTML(newpage)
        items=dom.xpath(‘//tr/td/a/text()‘)
        urls=dom.xpath(‘//tr/td/a/@href‘)
        assert (len(items)==len(urls))
        print(len(items))
        for i in range(len(urls)):
            print(items[i])
            new=(request.urlopen(urls[i]).read()).decode(decode.lower())
            ncs=re.findall(r‘<div id="endText" class="end-text">.*?</div>‘,data,re.S)
            newdom=etree.HTML(new)
            newitems=newdom.xpath("//div[@id=‘endText‘and @class=‘post_text‘]/p/text()")
            for n in newitems:
                print(n)
            print(‘=======================输入y继续‘)
            if ‘y‘==input():continue
            else:break;
时间: 2024-08-06 22:15:25

python爬新闻的相关文章

自动抓取163新闻的Python爬虫源码

Python爬虫的学习,自动抓取163新闻的Python爬虫源码,这是一个用Python语言编写的,自动抓取网易新闻的python爬虫实现方法一文. Python爬虫的抓取思路是:(1)通过分析目标新闻网址 ,分析其中以News.xxx.com 开头的链接(2)获取每一个链接的内容,并做整理合并到事前准备好的.txt 文本中,以便查看各新闻.但是需要注意的是:由于今天的测试对象,网易新闻的格式不是非常统一,所有会有部分漏掉的情况,还能大家见谅.也希望有能力的朋友们帮着改进一下. 自动抓取163新

Python 爬取广州商学院新闻----测试版

Python 爬取广州商学院新闻----测试版 程序简述:抓取广州商学院新闻栏目的全部新闻内容 开发环境:PyCharm Community Edition 爬取连接:http://news.gzcc.cn/ 作者:siberia 详细内容:http://easyboy1.blog.163.com/blog/static/2641120382017103105416810/ 详细内容:http://easyboy1.blog.163.com/blog/static/264112038201710

使用python爬取csdn博客访问量

最近学习了python和爬虫,想写一个程序练练手,所以我就想到了大家都比较关心的自己的博客访问量,使用python来获取自己博客的访问量,这也是后边我将要进行的项目的一部分,后边我会对博客的访问量进行分析,以折线图和饼图等可视化的方式展示自己博客被访问的情况,使自己能更加清楚自己的哪些博客更受关注,博客专家请勿喷,因为我不是专家,我听他们说专家本身就有这个功能. 一.网址分析 进入自己的博客页面,网址为:http://blog.csdn.net/xingjiarong 网址还是非常清晰的就是cs

python爬取某个网站的图片并保存到本地

python爬取某个网站的图片并保存到本地 #coding:utf-8 import urllib import re import sys reload(sys) sys.setdefaultencoding('gb2312') #获取整个页面的数据 def getHtml (url): page = urllib.urlopen(url) html = page.read() return html #保存图片到本地 def getImg(html): reg = r'src="(.+?\.

python爬爬爬之单网页html页面爬取

python爬爬爬之单网页html页面爬取 作者:vpoet 日期:大约在夏季 注:随意copy 不用告诉我 #coding:utf-8 import urllib2 Response=urllib2.urlopen("http://www.baidu.com"); Html=Response.read(); print Html; 运行结果: 再看看百度的源码: 是一样,没骗你吧

python爬取某个网页的图片-如百度贴吧

python爬取某个网页的图片-如百度贴吧 作者:vpoet 日期:大约在冬季 注:随意copy,不用告诉我 #coding:utf-8 import urllib import urllib2 import re if __name__ =="__main__": rex=r'src="(http://imgsrc.baidu.com/forum/w%3D580.*?\.jpg)"'; Response=urllib2.urlopen("http://t

使用python爬取MedSci上的影响因子排名靠前的文献

使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn/sci的交互过程.可以使用谷歌或火狐浏览器的“审查元素-->Network”,然后就可以看到操作页面就可以看到网站的交互信息.当在网页上点击“我要查询”时,网页会发送一个POST消息给服务器,然后,服务器返回查询结果 然后,将查询到的结果使用正则表达式提取出需要的数据. 最后将提取出的数据输出到文

Python爬取网页的三种方法

# Python爬取网页的三种方法之一:  使用urllib或者urllib2模块的getparam方法 import urllib fopen1 = urllib.urlopen('http://www.baidu.com').info() fopen2 = urllib2.urlopen('http://www.sina.com').info() print fopen1.getparam('charset') print fopen2.getparam('charset') #----有些

用python爬博客

用python爬博客 by 伍雪颖 以爬王垠的博客为例: import re import urllib2 def getHtmlCode(url): return urllib2.urlopen(url).read() def findTitleUrl(htmlString): regTitleUrl = re.compile("href=\"(.+?)\"") return regTitleUrl.findall(htmlString) def findTitl