python爬虫爬取csdn博客专家所有博客内容

简介: python爬虫爬取csdn博客专家所有博客内容: 全部过程采取自动识别与抓取,抓取结果是将一个博主的所有 文章存放在以其名字命名的文件内,代码如下 #coding:utf-8 import urllib2 from bs4 import BeautifulSoup im...
python爬虫爬取csdn博客专家所有博客内容:
全部过程采取自动识别与抓取,抓取结果是将一个博主的所有 文章存放在以其名字命名的文件内,代码如下

#coding:utf-8

import urllib2
from bs4 import BeautifulSoup
import os
import re
#import sys
#reload(sys)
#sys.setdefaultencoding("utf-8")


def getPage(href): #伪装成浏览器登陆,获取网页源代码
    headers = {  
        'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  
    }  
    req = urllib2.Request(  
        url = href ,
        headers = headers  
    )
    try:
        post = urllib2.urlopen(req)
    except urllib2.HTTPError,e:
        print e.code
        print e.reason
    return post.read()

url = 'http://blog.csdn.net/experts.html'

def getEvery(url):
    hrefList = []
    page = BeautifulSoup(getPage(url))
    div = page.find('div',class_='side_nav')
    liList = div.find_all('li')
    for li in liList:
        href = 'http://blog.csdn.net' + li.a.get('href')
        if href!='http://blog.csdn.net/experts.html':
            hrefList.append(href)
    return hrefList
#第一部分:得到首页博客专家各个系列链接
#===============================================================================
def getAll(href): #得到每个类别所有专家的姓名和博客首页地址
    page=BeautifulSoup(getPage(href))  #得到移动专家首页源代码,并beautifulsoup化
    div = page.find('div',class_='list_3',id='experts')
    for li in div.find_all('li'):
        name = li.get_text()
        href = li.a.get('href')
        getBlog(name,href)
#第二部分:得到每类所有专家的姓名和首页链接
#===============================================================================
def getPageNum(href):
    num =0
    page = getPage(href)
    soup = BeautifulSoup(page)
    div = soup.find('div',class_='pagelist')
    if div:
        result = div.span.get_text().split(' ')
        list_num = re.findall("[0-9]{1}",result[3])
        for i in range(len(list_num)):
            num = num*10 + int(list_num[i]) #计算总的页数
        return num
    else:
        return 0

def getText(name,url):
    page = BeautifulSoup(getPage(url))
    span_list = page.find_all('span',class_='link_title')
    div_list = page.find_all('div',class_='article_description')
    k =0
    str1 = 'none'
    fp = open("text\%s.txt" % name,"a")
    # 获取文章内容和内容
    for div in div_list:
        title = span_list[k].a.get_text().strip()
        text = div.get_text()
        title = title.encode('utf-8')  #转换成utf-8编码,否则后文写不到文件里
        text = text.encode('utf-8')
        #print title
        k+=1
        fp.write(str(title) + '\n' + str(text) + '\n')
        fp.write('===========================================' + '\n')
        
    fp.close()

def getBlog(name,href):
    i =1
    for i in range(1,(getPageNum(href)+1)):
        url = href + '/article/list/' + str(i)
        print url
        getText(name,url)
        i+=1
    print href,'======================================OK'
    
#第三部分:得到每类所有专家的博客内容链接
#===============================================================================


if __name__=="__main__":
    hrefList = getEvery(url)
    for href in hrefList:
        getAll(href)

结果如下:


相关文章
|
2月前
|
数据采集 Web App开发 数据可视化
Python爬虫分析B站番剧播放量趋势:从数据采集到可视化分析
Python爬虫分析B站番剧播放量趋势:从数据采集到可视化分析b
|
1月前
|
数据采集 数据挖掘 测试技术
Go与Python爬虫实战对比:从开发效率到性能瓶颈的深度解析
本文对比了Python与Go在爬虫开发中的特点。Python凭借Scrapy等框架在开发效率和易用性上占优,适合快速开发与中小型项目;而Go凭借高并发和高性能优势,适用于大规模、长期运行的爬虫服务。文章通过代码示例和性能测试,分析了两者在并发能力、错误处理、部署维护等方面的差异,并探讨了未来融合发展的趋势。
131 0
|
2月前
|
数据采集 存储 C++
Python异步爬虫(aiohttp)加速微信公众号图片下载
Python异步爬虫(aiohttp)加速微信公众号图片下载
|
1月前
|
数据采集 存储 JSON
地区电影市场分析:用Python爬虫抓取猫眼/灯塔专业版各地区票房
地区电影市场分析:用Python爬虫抓取猫眼/灯塔专业版各地区票房
|
1月前
|
数据采集 存储 Web App开发
Python爬虫库性能与选型实战指南:从需求到落地的全链路解析
本文深入解析Python爬虫库的性能与选型策略,涵盖需求分析、技术评估与实战案例,助你构建高效稳定的数据采集系统。
236 0
|
30天前
|
数据采集 监控 调度
应对频率限制:设计智能延迟的微信读书Python爬虫
应对频率限制:设计智能延迟的微信读书Python爬虫
|
1月前
|
数据采集 机器学习/深度学习 数据可视化
Python量化交易:结合爬虫与TA-Lib技术指标分析
Python量化交易:结合爬虫与TA-Lib技术指标分析
|
1月前
|
数据采集 存储 XML
Python爬虫XPath实战:电商商品ID的精准抓取策略
Python爬虫XPath实战:电商商品ID的精准抓取策略
|
2月前
|
数据采集 自然语言处理 分布式计算
大数据岗位技能需求挖掘:Python爬虫与NLP技术结合
大数据岗位技能需求挖掘:Python爬虫与NLP技术结合
|
2月前
|
数据采集 存储 NoSQL
Python爬虫案例:Scrapy+XPath解析当当网网页结构
Python爬虫案例:Scrapy+XPath解析当当网网页结构

热门文章

最新文章

推荐镜像

更多