簡體   English   中英

將列表存儲到 Python Sqlite3

[英]Storing a List into Python Sqlite3

我正在嘗試像這樣使用 Beautiful Soup 來抓取表單字段 ID

 for link in BeautifulSoup(content, parseOnlyThese=SoupStrainer('input')):
    if link.has_key('id'):
        print link['id']

讓我們假設它返回類似

username
email
password
passwordagain
terms
button_register

我想將其寫入 Sqlite3 DB。

我將在我的應用程序中做的是......使用這些表單字段的 ID 並嘗試做一個 POST 可能是。 問題是.. 有很多這樣的網站,我已經刮掉了它們的表單字段 ID。 所以關系是這樣的...

Domain1 - First list of Form Fields for this Domain1
Domain2 - Second list of Form Fields for this Domain2
.. and so on

我在這里不確定的是......我應該如何設計我的專欄來實現這種目的? 如果我只創建一個包含兩列的表可以嗎 - 比如說

COL 1 - Domain URL (as TEXT)
COL 2 - List of Form Field IDs (as TEXT)

要記住的一件事是......在我的應用程序中,我需要做這樣的事情......

偽代碼

If Domain is "http://somedomain.com":
    For ever item in the COL2 (which is a list of form field ids):
         Assign some set of values to each of the form fields & then make a POST request

請問哪位能指導一下?

於 2011 年 7 月 22 日編輯 - 我的以下數據庫設計是否正確?

我決定有這樣的解決方案。 你們有什么感想?

我將有如下三個表

表格1

Key Column (Auto Generated Integer) - Primary Key
Domain as TEXT

示例數據將類似於:

1   http://url1.com
2   http://url2.com
3   http://url3.com

表 2

Domain (Here I will be using the Key Number from Table 1)
RegLink - This will have the registeration link (as TEXT)
Form Fields (as Text)

示例數據將類似於:

1   http://url1.com/register    field1
1   http://url1.com/register    field2
1   http://url1.com/register    field3
2   http://url2.com/register    field1
2   http://url2.com/register    field2
2   http://url2.com/register    field3
3   http://url3.com/register    field1
3   http://url3.com/register    field2
3   http://url3.com/register    field3

表3

Domain (Here I will be using the Key Number from Table 1)
Status (as TEXT)
User (as TEXT)
Pass (as TEXT)

示例數據將類似於:

1   Pass    user1   pass1
2   Fail    user2   pass2
3   Pass    user3   pass3

你覺得這個餐桌設計好不好? 或者有什么可以改進的嗎?

您的表中存在規范化問題。

使用 2 個表

TABLE domains
int id primary key
text name

TABLE field_ids
int id primary key
int domain_id foreign key ref domains
text value

是一個更好的解決方案。

正確的數據庫設計會建議您有一個 URL 表和一個字段表,每個都引用 URL 記錄。 但根據您想對它們做什么,您可以將列表打包成一列。 有關如何了解 go 的信息,請參閱文檔

sqlite 是必需的嗎? 這可能不是存儲數據的最佳方式。 例如,如果您需要通過 URL 進行隨機訪問查找,擱置模塊可能是更好的選擇。 如果您只需要記錄它們並遍歷站點,則存儲為 CSV 可能更簡單。

試試這個來獲取ID:

ids = (link['id'] for link in
        BeautifulSoup(content, parseOnlyThese=SoupStrainer('input')) 
         if link.has_key('id'))

這應該向您展示如何保存它們,加載它們,並對它們做一些事情。 這使用單個表,並且只為每個域的每個字段插入一行。 這是最簡單的解決方案,完全適合相對較少的數據行。

from itertools import izip, repeat
import sqlite3

conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('''create table domains
(domain text, linkid text)''')

domain_to_insert = 'domain_name'
ids = ['id1', 'id2']
c.executemany("""insert into domains
      values (?, ?)""", izip(repeat(domain_to_insert), ids))
conn.commit()

domain_to_select = 'domain_name'
c.execute("""select * from domains where domain=?""", (domain_to_select,))

# this is just an example
def some_function_of_row(row):
    return row[1] + ' value'

fields = dict((row[1], some_function_of_row(row)) for row in c)
print fields
c.close()

使用str()將列表轉換為保存時的字符串。 然后使用eval()將其轉換回加載列表

試試這個,看看自己:

x = [1, 2]
print(type(x))
y = str(x)
print(type(y))
z = eval(y)
print(type(z))
print(x)
print(y)
print(z)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM