我正在寻找漫画网站。首先,我正在整理目录并创建包含名称、封面 URL 和漫画状态的
parent table
。
之后,我想为每部漫画创建 child tables
,其中包含章节列表、章节发布日期和章节 url。
下面是我正在使用的代码。不幸的是,子表根本没有创建,我不明白为什么。请帮助我错过的事情。
import sqlite3
from time import sleep
import requests
from bs4 import BeautifulSoup
connection = sqlite3.connect('parser_results.db')
cursor = connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Comics (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
cover TEXT NOT NULL,
status TEXT NOT NULL
)
''')
def get_url():
for count in range(1,2):
url = f"https://asuratoon.com/manga/?page={count}&order=update"
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
data_url = soup.find_all('div', class_='bsx')
for comic in data_url:
comic_url = comic.find('a').get('href')
yield comic_url
for comic_url in get_url():
response = requests.get(comic_url, headers=headers)
sleep(3)
soup = BeautifulSoup(response.text, "html.parser")
data_general = soup.find('div', class_='bixbox animefull')
name = data_general.find('h1', class_='entry-title').text
cover = data_general.find('img').get('src')
status = data_general.find('div', class_='imptdt').text
status = status.replace('Status ', '')
cursor.execute('INSERT INTO Comics (name, cover, status) VALUES (?, ?, ?)', (name, cover, status))
connection.commit()
cursor.execute('SELECT id FROM Comics')
ids = cursor.fetchall()
for id in ids:
cursor.execute('''CREATE TABLE IF NOT EXISTS ChildTable_%s (
id INTEGER PRIMARY KEY,
chapter_name TEXT,
release_date TEXT,
chapter_url TEXT
)''' % (ids[0]))
for comic_url in get_url():
response = requests.get(comic_url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser") #lxml
data = soup.find_all('div', class_='eph-num')
for i in data:
chapter_name = i.find('span', class_='chapternum').text
release_date = i.find('span', class_='chapterdate').text
chapter_url = i.find('a').get('href')
cursor.execute('INSERT INTO ChildTable_%s (chapter_name, release_date, chapter_url) VALUES (?, ?, ?)', (chapter_name, release_date, chapter_url))
connection.commit()
connection.close()
我尝试了上面的代码,但没有创建子表。我不知道我的任务还有哪些其他选择。
我减少了代码并使用了虚拟 ID 列表,但是如果您像我的示例一样更改子创建 sql 命令,它应该可以工作:
import sqlite3
connection = sqlite3.connect('parser_results.db')
cursor = connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Comics (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
cover TEXT NOT NULL,
status TEXT NOT NULL
)
''')
ids = [(1,"urlOne"),(2,"urlTwo")]
for id in ids:
child = "ChildTable_"+str(id[0])
cursor.execute(f"""CREATE TABLE IF NOT EXISTS [%s] (
id INTEGER PRIMARY KEY,
chapter_name TEXT,
release_date TEXT,
chapter_url TEXT
);""" % child)
connection.commit()
connection.close()