3 回答
TA贡献1797条经验 获得超4个赞
我认为使用 a 标签的类选择器来获得一个没有间隙的列表然后用熊猫转储到 csv 会更有效
from bs4 import BeautifulSoup
import requests
import pandas as pd
url = 'http://www.thebest100lists.com/best100actors/'
res = requests.get(url)
soup = BeautifulSoup(res.content, "lxml")
names = [name.text for name in soup.select('a.class1')]
df = pd.DataFrame(names,columns=['Names'])
df.to_csv(r'C:\Users\User\Desktop\Celebs.csv', sep=',', encoding='utf-8',index = False )
TA贡献1834条经验 获得超8个赞
你可以这样做:
celeblistsplit=celebList.split('\n')
celeblistsplit
进而:
f=open('output.csv','w')
for each in celeblistsplit:
if len(each)>0:
f.write(each)
f.write(',')
f.write('\n')
f.close()
结果文件:
Robert De Niro,
Al Pacino,
Tom Hanks,
Johnny Depp,
Jack Nicholson,
Marlon Brando,
Meryl Streep,
Leonardo DiCaprio,
...
TA贡献1795条经验 获得超7个赞
import bs4 as bs
import urllib.request
import csv
source = urllib.request.urlopen('http://www.thebest100lists.com/best100actors/').read()
soup = bs.BeautifulSoup(source, 'lxml')
celebList = [] # an empty list to store the text
for paragraph in soup.find_all('ol'):
celebList.append(paragraph.text)
# print(celebList)
# file writing
# print(celebList) # ["\nRobert De Niro\n\nAl Pacino\n\nTom Hanks\n\nJohnny .. ]
celebList = map(lambda s: s.strip(), celebList) # removing the leading spaces in the list
celebList = list(celebList)
with open('celebList.csv', 'w') as file:
for text in celebList:
file.write(text)
输出:
Robert De Niro
Al Pacino
Tom Hanks
Johnny Depp
Jack Nicholson
Marlon Brando
.
.
.
添加回答
举报