日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

脚本:获取CSDN文章的访问量

發布時間:2025/3/21 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 脚本:获取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
html = BeautifulSoup(response.text, 'html.parser')blog_list = html.select('.list_item_new > #article_list > .article_item')
  • 2.獲取article_title 以及文章鏈接
blog_title = house.select('.link_title > a')[0].string.encode('utf-8')blog_title = str(blog_title.replace(' ', '').replace('\n', ''))blog_url = urljoin(ADDR, house.select('.link_title > a')[0]['href'])
  • 3.獲取 link_view 閱讀人數 and link_comments 評論數
link_view = str(house.select('.article_manage > .link_view')[0])blog_people = re.search(r'\d+', re.search(r'\(\d+\)', link_view).group()).group()# 先獲取span然后實用正則提取閱讀人數link_comment = str(house.select('.article_manage > .link_comments')[0])blog_comment = re.search(r'\d+', re.search(r'\(\d+\)', link_comment).group()).group()# 先獲取span然后實用正則提取評論數

寫入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)
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的脚本:获取CSDN文章的访问量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。