python 爬取古詩文存入mysql數(shù)據(jù)庫的方法
使用正則提取數(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)應該寫成
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ù)庫的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Python 中數(shù)組和數(shù)字相乘時的注意事項說明
這篇文章主要介紹了Python 中數(shù)組和數(shù)字相乘時的注意事項說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05
linux環(huán)境下安裝pyramid和新建項目的步驟
這篇文章簡單介紹了linux環(huán)境下安裝pyramid和新建項目的步驟,大家參考使用2013-11-11
梳理總結(jié)Python開發(fā)中需要摒棄的18個壞習慣
大家好,今天給大家分享 18 個 Python 初學者常有的壞習慣,這些壞習慣不僅影響 Python 代碼的可讀性,而且 影響 Python 的運行性能,摒棄這些壞習慣并以 Pythonic 的方式編寫代碼,提高的不僅僅是你的代碼質(zhì)量,也給閱讀代碼的人留下好印象2022-01-01
Django框架設(shè)置cookies與獲取cookies操作詳解
這篇文章主要介紹了Django框架設(shè)置cookies與獲取cookies操作,結(jié)合實例形式詳細分析了Django框架針對cookie操作的各種常見技巧與操作注意事項,需要的朋友可以參考下2019-05-05

