1. 程式人生 > >python爬蟲爬取csdn部落格專家所有部落格內容

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)

相關推薦

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(hre

Python爬蟲CSDND首頁的所有的文章

  # -*- encoding: utf-8 -*- import re import urllib.request def function(): """Python爬蟲爬取CSDND首頁的所有的文章""" html="https://blog.csdn.n

python爬蟲NBA貼吧的所有精品貼

首先用直接的方法寫,先嚐試下能否爬取成功 #coding:utf-8 import urllib2,urllib import re ''' 1.準備url地址 2.準備請求頭 3.建立請求物件 4.發起請求獲取第一頁原始碼,接收響應 5.通過第一頁原始碼,找到總頁數和標題

Python爬蟲部落園作業

要求 第一部分: 請分析作業頁面,爬取已提交作業資訊,並生成已提交作業名單,儲存為英文逗號分隔的csv檔案。檔名為:hwlist.csv 。 檔案內容範例如下形式: 學號,姓名,作業標題,作業提交時間,作業URL 20194010101,張三,羊車門作業,2018-1

Python爬蟲一篇韓寒新浪部落

網上看到大神對Python爬蟲爬到很多有用的資訊,覺得很厲害,突然對想學Python爬蟲,雖然自己沒學過Python,但在網上找了一些資料看了一下,看到爬取韓寒新浪部落格的視訊,共三集,第一節講爬

Python爬蟲-糗事百科段子

hasattr com ima .net header rfi star reason images 閑來無事,學學python爬蟲。 在正式學爬蟲前,簡單學習了下HTML和CSS,了解了網頁的基本結構後,更加快速入門。 1.獲取糗事百科url http://www.qiu

python爬蟲頁面源碼在本頁面展示

一個 nts ring 想要 strip code 空白 列表 ngs python爬蟲在爬取網頁內容時,需要將內容連同內容格式一同爬取過來,然後在自己的web頁面中顯示,自己的web頁面為django框架 首先定義一個變量html,變量值為一段HTML代碼 >&

python 爬蟲 證券之星網站

爬蟲 周末無聊,找點樂子。。。#coding:utf-8 import requests from bs4 import BeautifulSoup import random import time #抓取所需內容 user_agent = ["Mozilla/5.0 (Windows NT 10.0

python爬蟲海量病毒文件

tle format nbsp contex logs request spl tde __name__ 因為工作需要,需要做深度學習識別惡意二進制文件,所以爬一些資源。 # -*- coding: utf-8 -*- import requests import re

Python爬蟲廣州大學教務系統的成績(內網訪問)

enc 用途 css選擇器 狀態 csv文件 表格 area 加密 重要 用Python爬蟲爬取廣州大學教務系統的成績(內網訪問) 在進行爬取前,首先要了解: 1、什麽是CSS選擇器? 每一條css樣式定義由兩部分組成,形式如下: [code] 選擇器{樣式} [/code

python爬蟲——古詩詞

爬蟲 古詩詞 實現目標 1.古詩詞網站爬取唐詩宋詞 2.落地到本地數據庫頁面分析 通過firedebug進行頁面定位: 源碼定位: 根據lxml etree定位div標簽:# 通過 lxml進行頁面分析 response = etree.HTML(data

利用Python爬蟲淘寶商品做數據挖掘分析實戰篇,超詳細教程

實戰 趨勢 fat sts AI top 名稱 2萬 安裝模塊 項目內容 本案例選擇>> 商品類目:沙發; 數量:共100頁 4400個商品; 篩選條件:天貓、銷量從高到低、價格500元以上。 項目目的 1. 對商品標題進行文本分析 詞雲可視化 2.

Python爬蟲 - 百度html代碼前200行

http src mage bsp bubuko str 百度 爬蟲 圖片 Python爬蟲 - 爬取百度html代碼前200行 - 改進版, 增加了對字符串的.strip()處理 Python爬蟲 - 爬取百度html代碼前200行

簡易python爬蟲boss直聘職位,並寫入excel

python爬蟲寫入excel1,默認城市是杭州,代碼如下#! -*-coding:utf-8 -*-from urllib import request, parsefrom bs4 import BeautifulSoupimport datetimeimport xlwt starttime = dat

Python 爬蟲微信文章

微信爬蟲 爬取微信文章 爬取公眾號文章搜狗微信平臺為入口 地址:http://weixin.sogou.com/ --------------------------------------------------------------搜索關鍵詞“科技”對比網址變化情況查看網址http://wei

python爬蟲QQ說說並且生成詞雲圖,回憶滿滿!

運維開發 網絡 分析 matplot 容易 jieba 編程語言 提示框 然而 Python(發音:英[?pa?θ?n],美[?pa?θɑ:n]),是一種面向對象、直譯式電腦編程語言,也是一種功能強大的通用型語言,已經具有近二十年的發展歷史,成熟且穩定。它包含了一組完善而且

Python爬蟲OA幸運飛艇平臺獲取數據

sta 獲取數據 status fail attrs color wrapper 排行榜 req 安裝BeautifulSoup以及requests 打開window 的cmd窗口輸入命令pip install requests 執行安裝,等待他安裝完成就可以了 Beaut

利用python爬蟲圖片並且制作馬賽克拼圖

python爬蟲 splay ise 做事 c-c sea mage item -a   想在妹子生日送妹子一張用零食(或者食物類好看的圖片)拼成的馬賽克拼圖,因此探索了一番= =。   首先需要一個軟件來制作馬賽克拼圖,這裏使用Foto-Mosaik-Edda(網上也有在

Python - 爬蟲和登陸github

用API搜尋GitHub中star數最多的前十個庫,並用post方法登陸並點選收藏 一 用API搜尋GitHub中star數最多的前十個庫 利用GitHub提供的API爬取前十個star數量最多的Python庫     GitHub提供了很多專門為爬蟲準

Python爬蟲豆瓣電影、讀書Top250並排序

更新:已更新豆瓣電影Top250的指令碼及網站 概述 經常用豆瓣讀書的童鞋應該知道,豆瓣Top250用的是綜合排序,除使用者評分之外還考慮了很多比如是否暢銷、點選量等等,這也就導致了一些近年來評分不高的暢銷書在這個排行榜上高高在上遠比一些經典名著排名還高,於是在這裡打算重新給To