In this script I'm parsing an html page using bs4
with urlopen('https://ahmia.fi/search/?q=' searchString) as webpage:
rawResaults = webpage.read().decode()
# Parsing the webpage to html using BeautifulSoup
parsed_html = BeautifulSoup(rawResaults, features="lxml")
then looping through each <li> element to get the value of element that is located inside that list item
for res in parsed_html.body.find_all_next('li', attrs={'class': 'result'}):
i = i 1
titel = str(res.find('a').text)
description = str(res.find('p').text)
description = description.strip()
description = description.replace("<p>","")
description = description.replace("<br>","")
link = str(res.find('cite').text)
lastCheckd = str(res.find('span').text)
What I want to do is combine all these strings into a JSON array which then will be sent to the frontend that will map through the JSON to create new components 'link' that take the props (titel,decription,link,lastChecked)
{/* {link ? (
link.map((lnk) => (
<Links
key={lnk._id}
titel={lnk.titel}
link={lnk.link}
description={lnk.description}
status={lnk.status}
/>
))
) : (
<Nolink />
)} */}
Here is what I've tried so far (I can't map through the result )
#How I declared 'dictz'
dictz = {}
#this part is inside the loop
dictz["link" str(i)]={
"titel": titel.strip(),
"description": (description[:75] '..') if len(description) > 75 else description ,
"link": link.strip(),
"lastCheckd": lastCheckd.strip(),
"status": "unknown"
}
# outside the loop
serialized_json = json.dumps(dictz)
An example result :
{
"link1": {
"titel": "hello, anon.",
"description": "hello, anon - Uncensored Onion ",
"link": "LINK",
"lastCheckd": "1, 3 ago",
"status": "unknown"
},
"link2": {
"titel": "Hello",
"description": "No description provided",
"link": "LINK",
"lastCheckd": "2, 1 ago",
"status": "unknown"
}
}
CodePudding user response:
You should be creating a list of dictionaries, not a dictionary. Instead of putting linkN
in the dictionary keys, it should be in the _id
key of the nested dictionaries.
dictz = []
...
dictz.append({
"_id": "link" str(i),
"titel": titel.strip(),
"description": (description[:75] '..') if len(description) > 75 else description ,
"link": link.strip(),
"lastCheckd": lastCheckd.strip(),
"status": "unknown"
})