戴帽子的小女孩 2021-10-21 16:59 采纳率: 66.7%
浏览 71
已结题

如何将python爬取下来的数据保存到Mysql


import requests
from lxml import etree
import pymysql
import re
headers={
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.59',
    }
url='https://www.gushiwen.cn/default_2.aspx'

def get_ancient_poetry():
   
    response=requests.get(url,headers=headers)
    content = response.content.decode('utf8')
    html = etree.HTML(content)
    titles=html.xpath('//div[@class="cont"]/p/a/b/text()')  #古诗名
    authors=html.xpath('//p[@class="source"]/a/text()') #作者
    dynastys=html.xpath('//p[@class="source"]/a/text()')  #朝代
    content=html.xpath('//div[@class="contson"]/text()') #古诗内容
    content=''.join(html.xpath('//div[@class="contson"]/text()')).strip()
    pomes = []
    print(content)
  




if __name__ == '__main__':
    get_ancient_poetry()

这里我打印content返回是正常的古诗内容

img

import requests
from lxml import etree
import pymysql
import re
headers={
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.59',
    }
url='https://www.gushiwen.cn/default_2.aspx'

def get_ancient_poetry():
    conn = pymysql.connect(host='localhost', user='root', password='123456', database='boss', port=3306)

    cursor = conn.cursor()
    response=requests.get(url,headers=headers)
    content = response.content.decode('utf8')
    html = etree.HTML(content)
    titles=html.xpath('//div[@class="cont"]/p/a/b/text()')  #古诗名
    authors=html.xpath('//p[@class="source"]/a/text()') #作者
    dynastys=html.xpath('//p[@class="source"]/a/text()')  #朝代
    content=html.xpath('//div[@class="contson"]/text()') #古诗内容
    content=''.join(html.xpath('//div[@class="contson"]/text()')).strip()
    pomes = []
    for value in zip(titles, authors, dynastys, content):
        title, author, dynasty, content = value
        pome = {
            '古诗名': title,
            '作者': author,
            '朝代': dynasty,
            '古诗内容': content
        }
        pomes.append(pome)
    for pome in pomes:
        print(pome)
        print('=' * 40)




if __name__ == '__main__':
    get_ancient_poetry()

这样子写之后在打印发现古诗内容就获取了一个字这是为啥该怎么修改代码

img

而且我还想保存到mysql数据库里面,表已经建好了但是不会能帮忙完善一下代码讲解一下吗

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2021-10-21 17:27
    关注

    根据后面处理数据的要求,content是一个列表,将第21行改成如下即可:
    content=[c.strip() for c in content]
    至于mysql在连接后,使用create table,insert into等语句将数据写入,参考sql及pymysql使用教程。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月29日
  • 已采纳回答 10月21日
  • 创建了问题 10月21日

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算