我已经在python中创建了一个脚本,以从网页中获取某些字段并将这些字段写入文本文件中。当我在csv文件或excel中写入数据时,我可以用正确的方式进行处理。但是,如果将数据写入文本文件,则会卡住。
到目前为止,我已经写过:
import requests
from bs4 import BeautifulSoup
url = "https://stackoverflow.com/questions/tagged/web-scraping"
res = requests.get(url)
soup = BeautifulSoup(res.text, "lxml")
for item in soup.select(".question-summary"):
name = item.select_one(".user-details a").text.strip()
title = item.select_one(".question-hyperlink").text.strip()
views = item.select_one(".views").text.strip()
print(name,title,views)
如何将已刮擦的字段写入文本文件,并使它们之间保持三个空格?
答案 0 :(得分:1)
使用str.format
。
例如:
import requests
from bs4 import BeautifulSoup
url = "https://stackoverflow.com/questions/tagged/web-scraping"
res = requests.get(url)
soup = BeautifulSoup(res.text, "lxml")
with open("your.txt", "w") as outfile: #Open file for write
for item in soup.select(".question-summary"):
name = item.select_one(".user-details a").text.strip()
title = item.select_one(".question-hyperlink").text.strip()
views = item.select_one(".views").text.strip()
print(name,title,views)
outfile.write("{} {} {}".format(name,title,views)) #Write Data with 3 space.