1

I have a list of output that are append from my web scraping.

for post in get_posts("test", pages=50):
    print(post)
    listposts.append(post)

Actual output:

{'post_id': '1', 'text': 'abc'}
{'post_id': '2', 'text': 'haha'}
{'post_id': '3', 'text': 'abc'}
{'post_id': '4', 'text': 'haha'}


How can make it to show only text with abc ? :

Expected output:

{'post_id': '1', 'text': 'abc'}
{'post_id': '3', 'text': 'abc'}
2

1 Answer 1

1

Add an if condition in your loop to conditionally append to your listposts list

for post in get_posts("test", pages=50):
    print(post)
    if "abc" in post["text"]: 
        listposts.append(post)

use if post["text"] == "abc": for exact match

2
  • Thanks for your reply, but it keep returning me the same value inside the listpost when i print it out Image
    – johnny2000
    Aug 4, 2021 at 9:47
  • @johnny2000 can't help more with what you tell me
    – azro
    Aug 4, 2021 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.