从Python到数据库或CSV

2024-03-29 05:01:19 发布

您现在位置:Python中文网/ 问答频道 /正文

对Python和scraping相当陌生,但到目前为止,我们已经将这些代码整合起来,将歌曲的作者和标题从网站上删除。你知道吗

当我运行代码时,我首先得到艺术家列表,然后是标题列表。你知道吗

我的问题是:如何将这些结果放入数据库或csv文件中?你知道吗

我有笔记本++为python工作,加上pycharm和idle,这段代码对这三个都适用。欢迎提出任何建议。你知道吗

from urllib import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.officialcharts.com/charts/singles-chart/19800203/7501/" )

bsObj = BeautifulSoup(html)
nameList = bsObj. findAll("div" , {"class" : "artist",})
for name in nameList:
print(name. get_text())

html = urlopen("http://www.officialcharts.com/charts/singles-chart/19800203/7501/" )
bsObj = BeautifulSoup(html)
nameList = bsObj. findAll("div" , {"class" : "title"})
for name in nameList:
print(name. get_text())

Tags: 代码namefromimportcomhttp标题列表
2条回答

或者您可以简单地使用pandas来创建csv函数:

import pandas as pd
from pandas import DataFrame as df
from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.officialcharts.com/charts/singles-chart/19800203/7501/" )

bsObj = BeautifulSoup(html)
DB = df(columns = ['artists','songs'])
artistList = bsObj.findAll("div", {"class": "artist"})
songList = bsObj.findAll("div", {"class": "title"})
DB['artists'] = [ a.getText().strip() for a in artistList ]
DB['songs'] = [ s.getText().strip() for s in songList ]

DB.to_csv('csvfile.csv')

这应该写入两列csv文件,其中第一列是艺术家,第二列是歌曲标题。你知道吗

import csv
from urllib import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.officialcharts.com/charts/singles-chart/19800203/7501/" )

bsObj = BeautifulSoup(html)
artistList = bsObj.findAll("div", {"class": "artist"})
songList = bsObj.findAll("div", {"class": "title"})
artists = [ a.getText().strip() for a in artistList ]
songs = [ s.getText().strip() for s in songList ]

with open('csvfile.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile, delimiter=",")
    for c in zip(artists, songs):
        writer.writerow(c)

相关问题 更多 >