我正在第一次使用網絡抓取工具,我正在使用美麗的湯來解析JSON文件並返回一些我發送給CSV的屬性。BeautifulSoup可以找到具有匹配值的元素嗎?
在JSON數組中,status
變量是一個二進制值(0/1)。我想只返回狀態爲0的數組。這樣做可行嗎?
"""soup = BeautifulSoup(html)
table = soup.find()
print soup.prettify()"""
js_data = json.loads(html)
Attraction = []
event = []
status = []
for doc in js_data["response"]["docs"]:
Attraction.append(doc["Attraction"])
event.append(doc["PostProcessedData"]["Onsales"]["event"]["date"])
status.append(doc["PostProcessedData"]["Onsales"]["status"])
with open("out.csv","w") as f:
datas = zip(Attraction,event,status)
keys = ["Attraction","event","status"]
f.write(";".join(keys))
for data in datas:
f.write(",".join([str(k).replace(",",";").replace("<br>"," ") for k in data]))
f.write("\n")
你嘗試過什麼,並沒有工作? –
添加html和你想要得到的東西,你爲什麼要用bs4來解析json? –