Skip to main content
 首页 » 编程设计

python之如何填充 Django sqlite3 数据库

2025年05月04日28mfryf

我的计划是从网站上批量收集律师传记数据,并将每批转换为.csv文件,然后转换为json,然后将每个文件加载到Django数据库中。

请让我知道如何以最佳方式完成这项任务。

请您参考如下方法:

直接加载数据库即可。批量采集网站数据,直接加载SQlite3。只需编写使用 Django ORM 的简单批处理应用程序。从网站收集数据并立即加载 SQLite3。不要创建 CSV。不要创建 JSON。不要创建中间结果。不要做任何额外的工作。


编辑。

from myapp.models import MyModel 
import urllib2 
 
with open("sourceListOfURLs.txt", "r" ) as source: 
    for aLine in source: 
        for this, the, the_other in someGenerator( aLine ): 
            object= MyModel.objects.create( field1=this, field2=that, field3=the_other ) 
            object.save() 
 
def someGenerator( url ): 
    # open the URL with urllib2 
    # parse the data with BeautifulSoup 
    yield this, that, the_other