版权声明:转载请注明出处 https://blog.csdn.net/qq_42292831/article/details/88901594
【 Python - 数据格式JSON化 】:介绍了数据JSON化的主要操作
源码:
import requests
import json
from pprint import pprint
from bs4 import BeautifulSoup
url = "https://blog.csdn.net/qq_42292831/article/category/8257708"
response = requests.get(url)
soup = BeautifulSoup(response.text,"html.parser")
a = soup.find_all("div",{"class":"article-item-box csdn-tracking-statistics"})
b = []
c = dict()
for data in a:
c[data.h4.a.text.strip()[1:].strip()] = data.h4.a.get("href")
b.append(c)
#print(b)
with open("json_test.json","w+",encoding="UTF-8") as f:
try:
json.dump(b,f,ensure_ascii=False,indent=4)
except BaseException as e:
print(e)
else:
print("Successful!")
with open("json_test.json","r",encoding="UTF-8") as f:
c = json.load(f)
# pprint(c)
#print(c[0])
for x in c[0].items():
pprint(x)