这个问题的解决方案可能是由于JSON数据的格式不正确。可能的解决方法是在逐个创建JSON文件时,确保将所有数据添加到JSON对象或列表中,并使用正确的JSON语法嵌套。以下是一种创建单个JSON文件的示例代码:
import requests
import json
from bs4 import BeautifulSoup
url = 'http://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
data = {}
# Scraping data from webpage and adding it to the dictionary
data['title'] = soup.find('title').text
data['description'] = soup.find('meta', attrs={'name': 'description'})['content']
data['keywords'] = soup.find('meta', attrs={'name': 'keywords'})['content']
# Saving the data to JSON file
with open('example.json', 'w') as outfile:
json.dump(data, outfile)
在上述例子中,我们首先定义了一个名称为“data”的空字典。然后,我们从网页中抓取数据并将其添加到字典中。最后,我们将字典保存到名为“example.json”的JSON文件中。 这样,在循环遍历每个URL时,可以确保每个文件中包含所有数据。