1 Star 0 Fork 0

杨谦/基于文件和数据库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
作业 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
杨谦 提交于 2021-10-13 03:00 . update 作业.
import requests
from lxml import etree
import sqlite3
url = 'http://book.zongheng.com/store.html'
res = requests.get(url)
# print(res.status_code)
xhtml = etree.HTML(res.text)
title = xhtml.xpath("//div[@class='bookname']/a/text()")
author = xhtml.xpath("//div[@class='bookilnk']/a[1]/text()")
type1 = xhtml.xpath("//div[@class='bookilnk']/a[2]/text()")
time1 = xhtml.xpath("//div[@class='bookilnk']/span[2]/text()")
linking = xhtml.xpath("//div[@class='bookimg']/a/img/@src")
print(title)
print(author)
# print(type1)
# print(time1)
# print(linking)
# with open("test.txt","a+") as f:
# # f.writelines([i+''+j+'\n'+''+k+'\n'+''+l+'\n'+''+m+'\n' for i,j,k,l,m in zip(title,author,type1,time,linking)])
# f.writelines([i+''+j+''+k for i,j,k in zip(title,author,type1)])
# import csv
# with open("test.csv","a+",newline='') as f:
# w=csv.writer(f)
# for i,j,k in zip(title,author,type1):
# w.writerow([i,j,k])
# f.writelines([i+''+j+'\n' for i,j in zip(title,author)])
dbname ='test.db'
conn =sqlite3.connect(dbname)
c =conn.cursor()
sql ='''
create table if not existsq novelTest (
bookname text not null primary key,
bookauthor text not null
);
'''
c.execute(sql)
#插入数据
insertDate ='''
INSERT INTO novelTest VALUES(?,?)
'''
for i,j in zip(title,author):
c.execute(insertDate,(i,j))
conn.commit()
conn.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qian-yang555/file-and-database-based.git
git@gitee.com:qian-yang555/file-and-database-based.git
qian-yang555
file-and-database-based
基于文件和数据库
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385