欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python如何爬取51cto數(shù)據(jù)并存入MySQL

 更新時間:2020年08月25日 11:18:17   作者:Keep__Studying  
這篇文章主要介紹了Python如何爬取51cto數(shù)據(jù)并存入MySQL,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

實驗環(huán)境

1.安裝Python 3.7

2.安裝requests, bs4,pymysql 模塊

實驗步驟1.安裝環(huán)境及模塊

可參考http://www.dbjr.com.cn/article/194104.htm

2.編寫代碼

# 51cto 博客頁面數(shù)據(jù)插入mysql數(shù)據(jù)庫
# 導入模塊
import re
import bs4
import pymysql
import requests

# 連接數(shù)據(jù)庫賬號密碼
db = pymysql.connect(host='172.171.13.229',
           user='root', passwd='abc123',
           db='test', port=3306,
           charset='utf8')
# 獲取游標
cursor = db.cursor()

def open_url(url):
  # 連接模擬網(wǎng)頁訪問
  headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
           'Chrome/57.0.2987.98 Safari/537.36'}
  res = requests.get(url, headers=headers)
  return res

# 爬取網(wǎng)頁內容
def find_text(res):
  soup = bs4.BeautifulSoup(res.text, 'html.parser')

  # 博客名
  titles = []
  targets = soup.find_all("a", class_="tit")
  for each in targets:
    each = each.text.strip()
    if "置頂" in each:
      each = each.split(' ')[0]
    titles.append(each)

  # 閱讀量
  reads = []
  read1 = soup.find_all("p", class_="read fl on")
  read2 = soup.find_all("p", class_="read fl")
  for each in read1:
    reads.append(each.text)
  for each in read2:
    reads.append(each.text)

  # 評論數(shù)
  comment = []
  targets = soup.find_all("p", class_='comment fl')
  for each in targets:
    comment.append(each.text)

  # 收藏
  collects = []
  targets = soup.find_all("p", class_='collect fl')
  for each in targets:
    collects.append(each.text)

   # 發(fā)布時間
  dates=[]
  targets = soup.find_all("a", class_='time fl')
  for each in targets:
    each = each.text.split(':')[1]
    dates.append(each)

  # 插入sql 語句
  sql = """insert into blog (blog_title,read_number,comment_number, collect, dates)
  values( '%s', '%s', '%s', '%s', '%s');"""
  # 替換頁面 \xa0
  for titles, reads, comment, collects, dates in zip(titles, reads, comment, collects, dates):
    reads = re.sub('\s', '', reads)
    comment = re.sub('\s', '', comment)
    collects = re.sub('\s', '', collects)
    cursor.execute(sql % (titles, reads, comment, collects,dates))
    db.commit()
    pass

# 統(tǒng)計總頁數(shù)
def find_depth(res):
  soup = bs4.BeautifulSoup(res.text, 'html.parser')
  depth = soup.find('li', class_='next').previous_sibling.previous_sibling.text
  return int(depth)

# 主函數(shù)
def main():
  host = "https://blog.51cto.com/13760351"
  res = open_url(host) # 打開首頁鏈接
  depth = find_depth(res) # 獲取總頁數(shù)

  # 爬取其他頁面信息
  for i in range(1, depth + 1):
    url = host + '/p' + str(i) # 完整鏈接
    res = open_url(url) # 打開其他鏈接
    find_text(res) # 爬取數(shù)據(jù)

  # 關閉游標
  cursor.close()
  # 關閉數(shù)據(jù)庫連接
  db.close()

if __name__ == '__main__':
  main()

3..MySQL創(chuàng)建對應的表

CREATE TABLE `blog` (
 `row_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
 `blog_title` varchar(52) DEFAULT NULL COMMENT '博客標題',
 `read_number` varchar(26) DEFAULT NULL COMMENT '閱讀數(shù)量',
 `comment_number` varchar(16) DEFAULT NULL COMMENT '評論數(shù)量',
 `collect` varchar(16) DEFAULT NULL COMMENT '收藏數(shù)量',
 `dates` varchar(16) DEFAULT NULL COMMENT '發(fā)布日期',
 PRIMARY KEY (`row_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

4.運行代碼,查看效果:

改進版:

改進內容:

1.數(shù)據(jù)庫里面的某些字段只保留數(shù)字即可

2.默認爬取的內容都是字符串,存放數(shù)據(jù)庫的某些字段,最好改為整型,方便后面數(shù)據(jù)庫操作

1.代碼如下:

import re
import bs4
import pymysql
import requests

# 連接數(shù)據(jù)庫
db = pymysql.connect(host='172.171.13.229',
           user='root', passwd='abc123',
           db='test', port=3306,
           charset='utf8')
# 獲取游標
cursor = db.cursor()

def open_url(url):
  # 連接模擬網(wǎng)頁訪問
  headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
           'Chrome/57.0.2987.98 Safari/537.36'}
  res = requests.get(url, headers=headers)
  return res

# 爬取網(wǎng)頁內容
def find_text(res):
  soup = bs4.BeautifulSoup(res.text, 'html.parser')

  # 博客標題
  titles = []
  targets = soup.find_all("a", class_="tit")
  for each in targets:
    each = each.text.strip()
    if "置頂" in each:
      each = each.split(' ')[0]
    titles.append(each)

  # 閱讀量
  reads = []
  read1 = soup.find_all("p", class_="read fl on")
  read2 = soup.find_all("p", class_="read fl")
  for each in read1:
    reads.append(each.text)
  for each in read2:
    reads.append(each.text)

  # 評論數(shù)
  comment = []
  targets = soup.find_all("p", class_='comment fl')
  for each in targets:
    comment.append(each.text)

  # 收藏
  collects = []
  targets = soup.find_all("p", class_='collect fl')
  for each in targets:
    collects.append(each.text)

  # 發(fā)布時間
  dates=[]
  targets = soup.find_all("a", class_='time fl')
  for each in targets:
    each = each.text.split(':')[1]
    dates.append(each)

  # 插入sql 語句
  sql = """insert into blogs (blog_title,read_number,comment_number, collect, dates)
  values( '%s', '%s', '%s', '%s', '%s');"""
  # 替換頁面 \xa0
  for titles, reads, comment, collects, dates in zip(titles, reads, comment, collects, dates):
    reads = re.sub('\s', '', reads)
    reads=int(re.sub('\D', "", reads)) #匹配數(shù)字,轉換為整型
    comment = re.sub('\s', '', comment)
    comment = int(re.sub('\D', "", comment)) #匹配數(shù)字,轉換為整型
    collects = re.sub('\s', '', collects)
    collects = int(re.sub('\D', "", collects)) #匹配數(shù)字,轉換為整型
    dates = re.sub('\s', '', dates)
    cursor.execute(sql % (titles, reads, comment, collects,dates))
    db.commit()
    pass

# 統(tǒng)計總頁數(shù)
def find_depth(res):
  soup = bs4.BeautifulSoup(res.text, 'html.parser')
  depth = soup.find('li', class_='next').previous_sibling.previous_sibling.text
  return int(depth)

# 主函數(shù)
def main():
  host = "https://blog.51cto.com/13760351"
  res = open_url(host) # 打開首頁鏈接
  depth = find_depth(res) # 獲取總頁數(shù)

  # 爬取其他頁面信息
  for i in range(1, depth + 1):
    url = host + '/p' + str(i) # 完整鏈接
    res = open_url(url) # 打開其他鏈接
    find_text(res) # 爬取數(shù)據(jù)

  # 關閉游標
  cursor.close()
  # 關閉數(shù)據(jù)庫連接
  db.close()

#主程序入口
if __name__ == '__main__':
  main()

2.創(chuàng)建對應表

CREATE TABLE `blogs` (
 `row_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
 `blog_title` varchar(52) DEFAULT NULL COMMENT '博客標題',
 `read_number` int(26) DEFAULT NULL COMMENT '閱讀數(shù)量',
 `comment_number` int(16) DEFAULT NULL COMMENT '評論數(shù)量',
 `collect` int(16) DEFAULT NULL COMMENT '收藏數(shù)量',
 `dates` varchar(16) DEFAULT NULL COMMENT '發(fā)布日期',
 PRIMARY KEY (`row_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

3.運行代碼,驗證

升級版

為了能讓小白就可以使用這個程序,可以把這個項目打包成exe格式的文件,讓其他人,使用電腦就可以運行代碼,這樣非常方便!

1.改進代碼:

#末尾修改為:
if __name__ == '__main__':
  main()
  print("\n\t\t所有數(shù)據(jù)已成功存放數(shù)據(jù)庫!!! \n")
  time.sleep(5)

2.安裝打包模塊pyinstaller(cmd安裝)

pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple/

3.Python代碼打包

1.切換到需要打包代碼的路徑下面

2.在cmd窗口運行 pyinstaller -F test03.py (test03為項目名稱)

4.查看exe包

在打包后會出現(xiàn)dist目錄,打好包就在這個目錄里面

5.運行exe包,查看效果

檢查數(shù)據(jù)庫

總結:

1.這一篇博客,是在上一篇的基礎上改進的,步驟是先爬取首頁的信息,再爬取其他頁面信息,最后在改進細節(jié),打包exe文件

2.我們爬取網(wǎng)頁數(shù)據(jù)大多數(shù)還是存放到數(shù)據(jù)庫的,所以這種方法很實用。

3.其實在此博客的基礎上還是可以改進的,重要的是掌握方法即可。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • python圖形繪制奧運五環(huán)實例講解

    python圖形繪制奧運五環(huán)實例講解

    在本文里我們給大家整理了一篇關于python圖形繪制奧運五環(huán)的實例內容,大家可以跟著學習下。
    2019-09-09
  • Django模板標簽中url使用詳解(url跳轉到指定頁面)

    Django模板標簽中url使用詳解(url跳轉到指定頁面)

    這篇文章主要介紹了Django模板標簽中url使用詳解(url跳轉到指定頁面),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • python實現(xiàn)簡單五子棋游戲

    python實現(xiàn)簡單五子棋游戲

    這篇文章主要為大家詳細介紹了python實現(xiàn)簡單五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • python跳出雙層循環(huán)的方法

    python跳出雙層循環(huán)的方法

    本文主要介紹了python跳出雙層循環(huán)的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • 詳解如何利用Numpy生成各種各樣的數(shù)組

    詳解如何利用Numpy生成各種各樣的數(shù)組

    Numpy提供了多種函數(shù)和方法,可以方便地生成不同形狀和類型的數(shù)組,今天小編就帶大家學習一下如何利用?Numpy?生成各種各樣的數(shù)組吧
    2023-07-07
  • 基于Python實現(xiàn)新聞爬取系統(tǒng)

    基于Python實現(xiàn)新聞爬取系統(tǒng)

    這篇文章主要為大家詳細介紹了如何基于Python實現(xiàn)一個簡單的新聞爬取系統(tǒng),文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下
    2023-06-06
  • 使用python BeautifulSoup庫抓取58手機維修信息

    使用python BeautifulSoup庫抓取58手機維修信息

    這篇文章主要介紹了一個使用python抓取58手機的精準商家信息,使用BeautifulSoup API的方法
    2013-11-11
  • python虛擬環(huán)境的安裝和配置(virtualenv,virtualenvwrapper)

    python虛擬環(huán)境的安裝和配置(virtualenv,virtualenvwrapper)

    這篇文章主要介紹了python虛擬環(huán)境的安裝和配置(virtualenv,virtualenvwrapper),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • linux安裝python修改默認python版本方法

    linux安裝python修改默認python版本方法

    在本文中我們給大家總結了關于linux安裝python修改默認python版本的方法和相關知識點,需要的讀者們參考下。
    2019-03-03
  • 10個Python實現(xiàn)的最頻繁使用的聚類算法

    10個Python實現(xiàn)的最頻繁使用的聚類算法

    聚類或聚類分析是無監(jiān)督學習問題。它通常被用作數(shù)據(jù)分析技術,用于發(fā)現(xiàn)數(shù)據(jù)中的有趣模式。本文為大家介紹了10個最頻繁使用的聚類算法,感興趣的可以了解一下
    2022-12-12

最新評論