脚本:获取CSDN文章的访问量
目標
- 獲取所有文章名,鏈接,閱讀人數,評論數
- 以適合pandas讀取的格式存儲之
分析
頁面跳轉
首頁:http://blog.csdn.net/fontthrone?viewmode=list
第二頁:http://blog.csdn.net/FontThrone/article/list/2
三四頁以此類推
根據第二三四頁的格式嘗試http://blog.csdn.net/FontThrone/article/list/1
成功跳轉:證明http://blog.csdn.net/fontthrone?viewmode=list = http://blog.csdn.net/FontThrone/article/list/1
那么獲取不同的頁面我們只需要通過跳轉鏈接最后面的 數字來控制就好了,真是簡單=- =
頁面整體構成
頁面構成(class)如圖所示
- article_list
- - list_item article_item
- - - article_title 標題
- - - - h1
- - - - - link_title
- - - - - - a
- - - article_description 文章摘要
- - - article_manage
- - - - link_postdate 日期
- - - - link_view 閱讀人數
- - - - link_comments 評論數
- - - - link_edit 編輯
- - - clear
我們首先獲取每一個- article_list,然后通過循環獲取每個list_item article_item中的信息
細節
- 使用bs4 解析網頁,減少了工作量
- a標簽中的href使用 [‘href’] 獲取
- 對于span中含有a標簽+text的,獲取text直接用正則進行獲取
- 注意編碼:1. 網頁獲取內容的編碼 1. py文件的默認編碼
代碼分解
獲取具體信息的方法
- 1.獲取article_list
- 2.獲取article_title 以及文章鏈接
- 3.獲取 link_view 閱讀人數 and link_comments 評論數
寫入CSV文件
import csv # 引入類庫 with open('info.csv', 'wb') as f:csv_writer = csv.writer(f, delimiter=',')# 創建-使用with,無需手動關閉csv_writer.writerow(['blog_title', 'blog_url', 'blog_people', 'blog_comment'])# 寫入內列名,便于pandas使用(按行寫入)csv_writer.writerow([blog_title, blog_url,blog_people, blog_comment]) #按行寫入 我們爬取的信息CODE
真·CODE
初代機參考:http://blog.csdn.net/fontthrone/article/details/75287311
# - * - coding: utf - 8 -*- # # 作者:田豐(FontTian) # 創建時間:'2017/8/5' # 郵箱:fonttian@Gmaill.com # CSDN:http://blog.csdn.net/fontthrone # from bs4 import BeautifulSoup from urlparse import urljoin import requests import csv import re import sys reload(sys) sys.setdefaultencoding('utf8')# account = str(raw_input('輸入csdn的登錄賬號:')) account = 'fontthrone'URL = 'http://blog.csdn.net/' + accountADDR = 'http://blog.csdn.net/' start_page = 0with open('info.csv', 'wb') as f:csv_writer = csv.writer(f, delimiter=',')csv_writer.writerow(['blog_title', 'blog_url', 'blog_people', 'blog_comment'])print 'starting'while True:start_page += 1URL2 = URL + '/article/list/' + str(start_page)print URL2response = requests.get(URL2)html = BeautifulSoup(response.text, 'html.parser')# print htmlblog_list = html.select('.list_item_new > #article_list > .article_item')# check blog_listif not blog_list:print 'No blog_list'breakfor house in blog_list:blog_title = house.select('.link_title > a')[0].string.encode('utf-8')blog_title = str(blog_title.replace(' ', '').replace('\n', ''))link_view = str(house.select('.article_manage > .link_view')[0])blog_people = re.search(r'\d+', re.search(r'\(\d+\)', link_view).group()).group()link_comment = str(house.select('.article_manage > .link_comments')[0])blog_comment = re.search(r'\d+', re.search(r'\(\d+\)', link_comment).group()).group()blog_url = urljoin(ADDR, house.select('.link_title > a')[0]['href'])csv_writer.writerow([blog_title, blog_url,blog_people, blog_comment])print 'ending'運行效果
與pandas結合,升級刷訪問量腳本初代機
說明
本代碼僅供學習參考,不建議使用該腳本進行訪問量的刷新
CODE
# blog_url =[ # 'http://blog.csdn.net/fontthrone/article/details/76675684', # 'http://blog.csdn.net/FontThrone/article/details/76652772', # 'http://blog.csdn.net/FontThrone/article/details/76652762', # 'http://blog.csdn.net/FontThrone/article/details/76652753', # 'http://blog.csdn.net/FontThrone/article/details/76652257', # 'http://blog.csdn.net/fontthrone/article/details/76735591', # 'http://blog.csdn.net/FontThrone/article/details/76728083', # 'http://blog.csdn.net/FontThrone/article/details/76727466', # 'http://blog.csdn.net/FontThrone/article/details/76727412', # 'http://blog.csdn.net/FontThrone/article/details/76695555', # 'http://blog.csdn.net/fontthrone/article/details/75805923', # ]import pandas as pd df1 = pd.DataFrame(pd.read_csv('info.csv'))blog_url = list(df1['blog_url'])補充
- 一代半有了,二代機還會遠嗎?
- 各位老鐵,一波666走起,(滑稽.jpg)
總結
以上是生活随笔為你收集整理的脚本:获取CSDN文章的访问量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python中的argparse模块
- 下一篇: TypeError: Can not c