代码拉取完成,页面将自动刷新
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。