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

python 爬取古詩文存入mysql數(shù)據(jù)庫的方法

 更新時間:2020年01月08日 08:36:33   作者:go_flush  
這篇文章主要介紹了python 爬取古詩文存入mysql數(shù)據(jù)庫的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下

使用正則提取數(shù)據(jù),請求庫requests,看代碼,在存入數(shù)據(jù)庫時,報錯ERROR 1054 (42S22): Unknown column ‘title' in ‘field list'。原來是我寫sql 有問題,sql = “insert into poem(title,author,content,create_time) values({},{},{},{})”.format(title, author,content,crate_time)
應(yīng)該寫成sql = “insert into poem(title,author,content,create_time) values('{}','{}','{}','{}')”.format(title, author,content,crate_time)。

把插入的值放入引號中。

import datetime
import re
import pymysql
import requests
url = "https://www.gushiwen.org/"
headers = {
 'User-Agent': "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"}
class Spiderpoem(object):
 conn = pymysql.Connect(host="localhost", port=3306, user="root", password='mysql', database='poem_data',
       charset="utf8")
 cs1 = conn.cursor()
 def get_requests(self, url, headers=None):
  """發(fā)送請求"""
  resp = requests.get(url, headers=headers)
  if resp.status_code == 200:
   # print(resp.request.headers)
   return resp.text
  return None
 def get_parse(self, response):
  """解析網(wǎng)頁"""
  re_data = {
   "title": r'<div\sclass="sons">.*?<b>(.*?)</b>.*?</div>',
   "author": r'<p>.*?class="source">.*?<a.*?>(.*?)</a>.*?<a.*?>(.*?)</a>.*?</p>',
   "content": r'<div\sclass="contson".*?>(.*?)</div>'
  }
  titles = self.reg_con(re_data["title"], response)
  authors = self.reg_con(re_data["author"], response)
  poems_list = self.reg_con(re_data["content"], response)
  contents = list()
  for item in poems_list:
   ite = re.sub(r'<.*?>|\s', "", item)
   contents.append(ite.strip())
  for value in zip(titles, authors, contents):
   title, author, content = value
   author = "".join([author[0], '.', author[1]])
   poem = {
    "title": title,
    "author": author,
    "content": content
   }
   yield poem
 def reg_con(self, params, response):
  """正則匹配"""
  if not response:
   return "請求錯誤"
  param = re.compile(params, re.DOTALL) # re.DOTALL 匹配換行等價于re.S
  result = re.findall(param, response)
  return result
 @classmethod
 def save_data(cls, poem):
  title = poem.get("title")
  author = poem.get("author")
  content = poem.get("content")
  crate_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  sql = "insert into poem(title,author,content,create_time) values('{}','{}','{}','{}')".format(title, author,
                          content,
                          crate_time)
  count = cls.cs1.execute(sql)
  print(count)
  cls.conn.commit()
 def main(self):
  resp = self.get_requests(url, headers)
  for it in self.get_parse(resp):
   self.save_data(it)
  self.cs1.close()
  self.conn.close()
if __name__ == '__main__':
 Spiderpoem().main()

總結(jié)

以上所述是小編給大家介紹的python 爬取古詩文存入mysql數(shù)據(jù)庫的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • python中的try except與R語言中的tryCatch異常解決

    python中的try except與R語言中的tryCatch異常解決

    這篇文章主要為大家介紹了python中的try except與R語言中的tryCatch異常解決的方式及分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • Python全局變量關(guān)鍵字global的簡單使用

    Python全局變量關(guān)鍵字global的簡單使用

    python中g(shù)lobal關(guān)鍵字主要作用是聲明變量的作用域,下面這篇文章主要給大家介紹了關(guān)于Python全局變量關(guān)鍵字global的簡單使用,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Django基于客戶端下載文件實(shí)現(xiàn)方法

    Django基于客戶端下載文件實(shí)現(xiàn)方法

    這篇文章主要介紹了Django基于客戶端下載文件實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Python中實(shí)現(xiàn)常量(Const)功能

    Python中實(shí)現(xiàn)常量(Const)功能

    這篇文章主要介紹了Python中實(shí)現(xiàn)常量(Const)功能,python語言本身沒有提供const,本文使用一個類來實(shí)現(xiàn)常量定義功能,并介紹了使用方法,需要的朋友可以參考下
    2015-01-01
  • Python協(xié)程原理全面分析

    Python協(xié)程原理全面分析

    協(xié)程(co-routine,又稱微線程、纖程)是一種多方協(xié)同的工作方式。協(xié)程不是進(jìn)程或線程,其執(zhí)行過程類似于Python函數(shù)調(diào)用,Python的asyncio模塊實(shí)現(xiàn)的異步IO編程框架中,協(xié)程是對使用async關(guān)鍵字定義的異步函數(shù)的調(diào)用
    2023-02-02
  • Pytorch的mean和std調(diào)查實(shí)例

    Pytorch的mean和std調(diào)查實(shí)例

    今天小編就為大家分享一篇Pytorch的mean和std調(diào)查實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python中生成隨機(jī)整數(shù)的三種方法

    Python中生成隨機(jī)整數(shù)的三種方法

    本文介紹了如何在Python中生成一到一百隨機(jī)整數(shù)的多種方法,包括random庫,NumPy庫和random.sample函數(shù)這三種方法,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • python實(shí)現(xiàn)逆序輸出一個數(shù)字的示例講解

    python實(shí)現(xiàn)逆序輸出一個數(shù)字的示例講解

    今天小編就為大家分享一篇python實(shí)現(xiàn)逆序輸出一個數(shù)字的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python加載自定義詞典實(shí)例

    python加載自定義詞典實(shí)例

    今天小編就為大家分享一篇python加載自定義詞典實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python計算一個序列的平均值的方法

    python計算一個序列的平均值的方法

    這篇文章主要介紹了python計算一個序列的平均值的方法,涉及Python遞歸遍歷與數(shù)學(xué)計算的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07

最新評論