I have a list of dictionaries which is a scrape of HTML elements from a forum:
[{'title': 'aaa', 'responses': '1 response', 'url': 'https://www.example.com/aaa'}, {'title': 'bbb', 'responses': '4 responses', 'url': 'https://www.example.com/bbb'}, {'title': 'ccc', 'responses': '2 responses', 'url': 'https://www.example.com/ccc'}, {'title': 'ddd', 'responses': '8 responses', 'url': 'https://www.example.com/ccc'}, {'title': 'eee', 'responses': '2 responses', 'url': 'https://www.example.com/eee'}]
I want to extract the 'url' items from the dictionaries if the 'reponses' item is smaller than 2 responses. I try this code which works well but only for the first item:
dict = {'url1': ''}
item1 = "1 response"
item2 = "2 responses"
if item1 in scrape[0]['responses'] or item2 in scrape[0]['responses']:
dict['url1'] = scrape[0]['url']
print(dict)
{'url1': 'https://www.example.com/aaa'}
If I delete the [0] element I get this: TypeError: list indices must be integers or slices, not str
I want this result:
{'url1': 'https://www.example.com/aaa', 'url2': 'https://www.example.com/eee'}
How to do this?