无法在文本文件中写入多个字段

时间:2019-07-04 09:07:53

标签: python python-3.x web-scraping

我已经在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)

如何将已刮擦的字段写入文本文件,并使它们之间保持三个空格?

1 个答案:

答案 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.