我想从Hindustantimes.com中提取新闻头条。我有以下代码,但我在同一行的不同列中获取了数据。我希望每个新闻都分开显示。谁能帮帮我吗。提前致谢。这是我的代码:
import requests
import bs4
import csv
res=requests.get('https://www.hindustantimes.com')
soup=bs4.BeautifulSoup(res.text,'lxml')
x=[]
for i in soup.select('div.subhead4'):
x.append(i.text)
for i in soup.select('div.bigstory-mid-h3'):
x.append(i.text)
for i in x:
print(i)
with open('newz.csv','w') as cF:
wr = csv.writer(cF)
wr.writerow(x)
答案 0 :(得分:4)
writerow
需要反复编写:
writerow(['a', 'b', 'c'])
-> a,b,c
此外,您无需在写入之前将数据附加到列表中。
stories = soup.select('div.subhead4') + soup.select('div.bigstory-mid-h3')
with open('output.csv', 'w') as f:
writer = csv.writer(f)
for headline in stories:
writer.writerow([headline.text.strip()])
答案 1 :(得分:1)
res = requests.get('https://www.hindustantimes.com')
soup = bs4.BeautifulSoup(res.text,'lxml')
x=[]
for i in soup.select('div.subhead4'):
x.append(i.text.strip())
for i in soup.select('div.bigstory-mid-h3'):
x.append(i.text.strip())
with open('newz.csv','w') as cF:
wr = csv.writer(cF)
for i in x:
wr.writerow([i])
答案 2 :(得分:0)
简单的.csv文件,此处不使用csv模块
with open('newz.csv','w') as cF:
for x in soup.select('.subhead4 a'):
cF.write(x.text + '\n')