Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python - Filter results in seatch output issue

I'm using the Google Custom Search API, and for a specific purpose, I need to separate the social sites from the non-social ones. Here's my code:

for result in results:
    omitSocials = ['instagram', 'twitter', 'facebook', 'amazon', 'linkedin', 'youtube', 'vimeo', 'google', 'pinterest']
    #omitSocials = ['https://www.instagra', 'https://twitt', 'https://www.faceboo', 'https://www.pinterest', 
                   #'https://business.faceboo', 'https://www.amazon', 'https://m.faceboo', 'https://vimeo', 
                   #'https://www.youtu', 'https://play.google', 'https://support.goog']
    urls = str(result.get('link'))
    for social in omitSocials:
        if social in urls:
            print("="*10, f"Result", "="*10)
            print(result.get('title'))
            print(urls)
            print(result.get('snippet'))
        else:
            pass

Output:

========== Result ==========
Pinch of Yum (@pinchofyum) ? Instagram photos and videos
https://www.instagram.com/pinchofyum/?hl=en
1m Followers, 226 Following, 2435 Posts - See Instagram photos and videos 
from Pinch of Yum (@pinchofyum)
========== Result ==========
Pinch of Yum (pinchofyum) on Pinterest
https://www.pinterest.com/pinchofyum/
Pinch of Yum - 985.54k Followers, 446 Following, 54382 pins | Pinch of Yum - a 
food blog with simple, tasty, and (mostly) healthy recipes.
========== Result ==========
Pinch of Yum (@pinchofyum) | Twitter
https://twitter.com/pinchofyum?lang=en
The latest Tweets from Pinch of Yum (@pinchofyum). Simple, tasty, and (mostly) 
healthy recipes. Saint Paul, MN.
========== Result ==========
Pinch of Yum - Home | Facebook
https://www.facebook.com/pinchofyum/
Pinch of Yum, Minneapolis, MN. 354419 likes · 801 talking about this. Simple, 
tasty, and (mostly) healthy recipes. Recipes:...
========== Result ==========
Pinch of Yum
https://www.youtube.com/channel/UC9ORxqKBywaO7fgflFJzHRw
Videos from Pinch of Yum - A food blog with simple, tasty, and (mostly) healthy 
recipes. ... A food blog with simple and tasty recipes. https://pinchofyum.com/?.

But when I do the same thing for for social not in urls:, I'm not getting the result without the social sites. Every result (including the social ones) is occurring, but 9 times each! Desired Output:

========== Result ==========
Pinch of Yum - A food blog with simple and tasty recipes.
https://pinchofyum.com/
... your food photography4 Behind the Scenes: 5 Time-Saving Tips for Food 
Photography5 Our First Ever Tasty Food Photography Workshop · Pinch of Yum.
========== Result ==========
Recipes - Pinch of Yum
https://pinchofyum.com/recipes
Pinch of Yum is a food blog with hundreds of simple, healthy recipes to fit just 
about any taste or diet. Find all our recipes here!
========== Result ==========
Lindsay Ostrom (@pinchofyum) Profile, Photos & Recipes | The ...
https://thefeedfeed.com/pinchofyum
We like to eat.
https://pinchofyum.tumblr.com/
Jun 22, 2013 ... pinchofyum, a blog on Tumblr. Never miss a post from pinchofyum. Make gifs, 
join group chats, find your community. Only in the app.
========== Result ==========
pinchofyum | Healthy Aperture
https://healthyaperture.com/author/pinchofyum
A unique recipe discovery site curated by registered dietitians helping you find 
the best healthy food blog recipes - including gluten free, vegan and more.

This output is the one I want to get, it does not include the socials.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can replace your for loop and if statement with the following. This will check if any of the strings in omitSocials is in your current url.

if all(social not in urls for social in omitSocials):
    print("="*10, f"Result", "="*10)
    print(result.get('title'))
    print(urls)
    print(result.get('snippet'))
else:
    pass

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...