【发布时间】:2018-06-23 11:18:39
【问题描述】:
编写了一个简单的网络抓取脚本来解析某个新闻频道的推文。所以我希望它解析这些推文并将其写入.csv 文件中。该脚本似乎运行良好,但我只是不知道如何让它在各自的标题下写入“tweets”和“news_link”!
我错过了什么?
代码:
import urllib.request
import bs4
import csv
source = urllib.request.urlopen("https://twitter.com/abpnewstv").read()
soup = bs4.BeautifulSoup(source, "lxml")
with open("twitter news.csv", "w", newline="") as csvfile:
news_writer = csv.writer(csvfile, delimiter=",")
news_writer.writerow(["tweet", "news_link"])
for content in soup.find_all("div", {"class": "js-tweet-text-container"}):
tweet = content.p.text.split(".")[0]
print(tweet)
try:
news_link = content.a.text
except AttributeError:
pass
print(news_link + "\n")
【问题讨论】:
标签: python python-3.x csv twitter