python 爬取古詩(shī)文存入mysql數(shù)據(jù)庫(kù)的方法
使用正則提取數(shù)據(jù),請(qǐng)求庫(kù)requests,看代碼,在存入數(shù)據(jù)庫(kù)時(shí),報(bào)錯(cuò)ERROR 1054 (42S22): Unknown column ‘title' in ‘field list'。原來(lái)是我寫sql 有問(wèn)題,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)。
把插入的值放入引號(hào)中。
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ā)送請(qǐng)求"""
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)頁(yè)"""
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 "請(qǐng)求錯(cuò)誤"
param = re.compile(params, re.DOTALL) # re.DOTALL 匹配換行等價(jià)于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 爬取古詩(shī)文存入mysql數(shù)據(jù)庫(kù)的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Python 中數(shù)組和數(shù)字相乘時(shí)的注意事項(xiàng)說(shuō)明
這篇文章主要介紹了Python 中數(shù)組和數(shù)字相乘時(shí)的注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05
linux環(huán)境下安裝pyramid和新建項(xiàng)目的步驟
這篇文章簡(jiǎn)單介紹了linux環(huán)境下安裝pyramid和新建項(xiàng)目的步驟,大家參考使用2013-11-11
梳理總結(jié)Python開發(fā)中需要摒棄的18個(gè)壞習(xí)慣
大家好,今天給大家分享 18 個(gè) Python 初學(xué)者常有的壞習(xí)慣,這些壞習(xí)慣不僅影響 Python 代碼的可讀性,而且 影響 Python 的運(yùn)行性能,摒棄這些壞習(xí)慣并以 Pythonic 的方式編寫代碼,提高的不僅僅是你的代碼質(zhì)量,也給閱讀代碼的人留下好印象2022-01-01
Python進(jìn)階之自定義對(duì)象實(shí)現(xiàn)切片功能
這篇文章主要介紹了Python進(jìn)階之自定義對(duì)象實(shí)現(xiàn)切片功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Django框架設(shè)置cookies與獲取cookies操作詳解
這篇文章主要介紹了Django框架設(shè)置cookies與獲取cookies操作,結(jié)合實(shí)例形式詳細(xì)分析了Django框架針對(duì)cookie操作的各種常見技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-05-05
學(xué)習(xí)Python爬蟲前必掌握知識(shí)點(diǎn)
這篇文章主要介紹了學(xué)習(xí)Python爬蟲前,我們需要了解涉及爬蟲的知識(shí)點(diǎn),學(xué)習(xí)爬蟲的知識(shí)點(diǎn)比較多,我們一起學(xué)習(xí)爬蟲吧2021-04-04
Python獲取央視節(jié)目單的實(shí)現(xiàn)代碼
這篇文章主要介紹了Python獲取央視節(jié)目單的實(shí)現(xiàn)代碼,涉及Python頁(yè)面采集的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
利用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Web匯率計(jì)算器
Dash?是一個(gè)用于構(gòu)建基于?Web?的應(yīng)用程序的?Python?庫(kù),無(wú)需?JavaScript?。本文將利用Dash編寫一個(gè)簡(jiǎn)單的Web匯率計(jì)算器,感興趣的可以了解一下2022-08-08

