文章目录
- 关于 SerpAPI
- serpapi & google-search-results
- http 请求示例:
- Python 调用示例
- Basic Examples in Python
- Search Bing
- Search Baidu
- Search Yahoo
- Search YouTube
- Search Walmart
- Search eBay
- Search Naver
- Search Home Depot
- Search Apple App Store
- Search DuckDuckGo
- Search Google
- Search Google Scholar
- Search Google Autocomplete
- Search Google Product
- Search Google Reverse Image
- Search Google Events
- Search Google Local Services
- Search Google Maps
- Search Google Jobs
- Search Google Play
- Search Google Images
关于 SerpAPI
- 官网:https://serpapi.com
- 官方文档:https://serpapi-python.readthedocs.io/en/latest/
- PYPI : https://pypi.org/project/serpapi/
- github : https://github.com/serpapi
- Google Search Results in Python
https://github.com/serpapi/google-search-results-python - serpapi-python
https://github.com/serpapi/serpapi-python
- Google Search Results in Python
- LangChain - Serpapi https://www.langchain.com.cn/ecosystem/serpapi
This repository is the home of the soon–to–be official Python API wrapper for SerpApi. This serpapi
module allows you to access search data in your Python application.
SerpApi supports Google, Google Maps, Google Shopping, Bing, Baidu, Yandex, Yahoo, eBay, App Stores, and more. Check out the documentation for a full list.
serpapi & google-search-results
Please note that this package is separate from the legacy serpapi
module, which is available on PyPi as google-search-results
.
This package is maintained by SerpApi, and is the recommended way to access the SerpApi service from Python.
有四种方案,其中免费的如下
我的CN手机号,没有收到验证码,所以注册失败
http 请求示例:
https://serpapi.com/search.json?q=Coffee&location=Austin,+Texas,+United+States&hl=en&gl=us&google_domain=google.com
https://serpapi.com/search.json?q=Coffee&location=Brazil&hl=pt&gl=br&google_domain=google.com.br
Python 调用示例
pip install serpapi
from serpapi import GoogleSearchparams = {"q": "Coffee","location": "Austin, Texas, United States","hl": "en","gl": "us","google_domain": "google.com","api_key": "secret_api_key"
}search = GoogleSearch(params)
results = search.get_dict()
Let’s start by searching for Coffee on Google:
>>> import serpapi
>>> s = serpapi.search(q="Coffee", engine="google", location="Austin, Texas", hl="en", gl="us")
# s 是一个 SerpResults 对象,像是一个标准 dict,上面添加了一些便捷的功能# 打印日志:
>>> s["organic_results"][0]["link"]
'https://en.wikipedia.org/wiki/Coffee'# 打印第一个结果,以 Python 的方式
>>> s["organic_results"][0].get("title")
'Coffee - Wikipedia'
s 内容如下:
{"search_metadata": {"id": "6602701cfe41d084fd3ed5da","status": "Success","json_endpoint": "https://serpapi.com/searches/a15e1b92727f292c/6602701cfe41d084fd3ed5da.json","created_at": "2024-03-26 06:50:04 UTC","processed_at": "2024-03-26 06:50:04 UTC","google_url": "https://www.google.com/search?q=Coffee&oq=Coffee&uule=w+CAIQICIdQXVzdGluLFRYLFRleGFzLFVuaXRlZCBTdGF0ZXM&hl=en&gl=us&sourceid=chrome&ie=UTF-8","raw_html_file": "https://serpapi.com/searches/a15e1b92727f292c/6602701cfe41d084fd3ed5da.html","total_time_taken": 7.62},"search_parameters": {"engine": "google","q": "Coffee","location_requested": "Austin, Texas","location_used": "Austin,TX,Texas,United States","google_domain": "google.com","hl": "en","gl": "us","device": "desktop"},"search_information": {"query_displayed": "Coffee","total_results": 5760000000,"time_taken_displayed": 0.37,"organic_results_state": "Results for exact spelling"},"local_map": {"link": "https://www.google.com/search?sca_esv=6cb148437a7a9a38&gl=us&hl=en&q=Coffee&npsic=0&rflfq=1&rldoc=1&rllag=31994604,-99807361,55029&tbm=lcl&sa=X&ved=2ahUKEwizk5Ccq5GFAxUDjokEHeceA94QtgN6BAglEAE","image": "https://serpapi.com/searches/6602701cfe41d084fd3ed5da/images/2677b372a72544dbbc26036d5c7b09db.png","gps_coordinates": {"latitude": 31.994604,"longitude": -99.807361,"altitude": 55029}},"local_results": {"places": [{"position": 1,"rating": 4.7,"reviews_original": "(63)","reviews": 63,"description": "Dine-in\u00b7Drive-through\u00b7No delivery","place_id": "16666839338430965111","place_id_search": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=Austin%2C+Texas&ludocid=16666839338430965111&q=Coffee","lsig": "AB86z5WD7smQJJZYj7Eh1KXWdsl2","thumbnail": "https://serpapi.com/searches/6602701cfe41d084fd3ed5da/images/47f37fc32e9075402698b02afcfe658f15416b9c44ead714051999e4f099a16f03170b796c045734.jpeg","service_options": {"dine_in": true,"drive_through": true,"no_delivery": true},"title": "The Branch Coffeehouse","type": "Coffee shop","address": "Tuscola, TX","hours": "Closed \u22c5 Opens 6 AM"},{"position": 2,"rating": 4.3,...},{"position": 3,"rating": 4.8,...}],"more_locations_link": "https://www.google.com/search?sca_esv=6cb148437a7a9a38&gl=us&hl=en&tbs=lf:1,lf_ui:9&tbm=lcl&q=Coffee&rflfq=1&num=10&uule=w+CAIQICIdQXVzdGluLFRYLFRleGFzLFVuaXRlZCBTdGF0ZXM&sa=X&ved=2ahUKEwizk5Ccq5GFAxUDjokEHeceA94QjGp6BAgrEAE"},"immersive_products": [{"thumbnail": "https://serpapi.com/searches/6602701cfe41d084fd3ed5da/images/49e6224fb883d7093d9d5db8b6707fcdcb9bf962cec4743d996f7845da535981.webp","source": "Amazon.com, 10+","title": "Folgers Classic Roast Ground Coffee","price": "$4.49","extracted_price": 4.49,"immersive_product_page_token": "eyJlaSI6IkluQU...lZSJ9","serpapi_link": "https://serpapi.com/search.json?engine=google_immersive_product&page_token=eyJlaS...UENfZmZlZSJ9"},{"thumbnail": "https://serpapi.com/searches/6602701cfe41d084fd3ed5da/images/49e6224fb883d7093d9d5db8b6707fcd77fcca065cb1ab76d1fc85c40626c4b4.webp","source": "Black Rifle Coffee Company, 10+","title": "Black Rifle Coffee Company Murdered Out Coffee Roast","price": "$16.99","extracted_price": 16.99,"immersive_product_page_token": "eyJlaSI6IkluQUNaclA2QklPY3B0UV....UFJPRF9QQ18xNTQ3NzA1NTU4Nzk2OTQ3MTc4NSIsInF1ZXJ5IjoiQ29mZmVlIn0=","serpapi_link": "https://serpapi.com/search.json?engine=google_immer...oiQ29mZmVlIn0%3D"},{..."title": "Bones Coffee Company New Flavors! Favorite Flavors Sample Pack 4 oz Pack of 5 Assorted Coffee Beans",...},...{..."title": "Starbucks Breakfast Blend Medium Roast Ground Coffee",...}],"related_questions": [{"question": "Is coffee actually healthy for you?","snippet": "Hu said that moderate coffee intake\u2014about 2\u20135 cups a day\u2014is linked to a lower likelihood of type 2 diabetes, heart disease, liver and endometrial cancers, Parkinson's disease, and depression. It's even possible that people who drink coffee can reduce their risk of early death.","title": "Is coffee good or bad for your health? | News - hsph.harvard.edu","link": "https://www.hsph.harvard.edu/news/hsph-in-the-news/is-coffee-good-or-bad-for-your-health/#:~:text=Hu%20said%20that%20moderate%20coffee,their%20risk%20of%20early%20death.","displayed_link": "https://www.hsph.harvard.edu \u203a news \u203a hsph-in-the-news","source_logo": "https://serpapi.com/searches/6602701cfe41d084fd3ed5da/images/b460e89a15194171da0396937f5d7133523eb116b317ef2548aa4c977ab879e3.png","next_page_token": "eyJvbnMiOiIx...XzIifQ==","serpapi_link": "https://serpapi.com/search.json?device=desktop&engine=..."},...{"question": "What are the 4 types of coffee?","snippet": "Learn about the four most popular types of coffee beans\u2014Arabica, Robusta, Excelsa and Liberica\u2014and when to use them.","title": "4 Types of Coffee Beans: How to Choose - KitchenAid",...}],"organic_results": [{"position": 1,"title": "Coffee","link": "https://en.wikipedia.org/wiki/Coffee","redirect_link": "https://www.google.comhttps://en.wikipedia.org/wiki/Coffee","displayed_link": "https://en.wikipedia.org \u203a wiki \u203a Coffee","thumbnail": "https://serpapi.com/searches/6602701cfe41d084fd3ed5da/images/19875953fd2763176ecb268a71b09fd91b6fe1da2d76a8e70422cfe845dba055.jpeg","favicon": "https://serpapi.com/searches/6602701cfe41d084fd3ed5da/images/19875953fd2763176ecb268a71b09fd980eab7533207994f46f9cbdee41bc698.png","snippet": "Coffee is a beverage brewed from roasted coffee beans. Darkly colored, bitter, and slightly acidic, coffee has a stimulating effect on humans, primarily due ...","snippet_highlighted_words": ["Coffee","coffee","coffee"],"sitelinks": {"inline": [{"title": "History","link": "https://en.wikipedia.org/wiki/History_of_coffee"},{"title": "Coffee bean","link": "https://en.wikipedia.org/wiki/Coffee_bean"},{"title": "Coffee production","link": "https://en.wikipedia.org/wiki/Coffee_production"},{"title": "Coffee preparation","link": "https://en.wikipedia.org/wiki/Coffee_preparation"}]},"source": "Wikipedia"},...{"position": 6,"title": "Counter Culture Coffee",..."snippet_highlighted_words": ["Coffee","coffee","coffee","coffee"],"sitelinks": {"inline": [{"title": "Our Coffees","link": "https://counterculturecoffee.com/collections/coffee"},...{"title": "Subscriptions","link": "https://counterculturecoffee.com/pages/on-going-subscription"}]},"rich_snippet": {"top": {"detected_extensions": {"free_day_delivery": 1},"extensions": ["Free 1\u20134 day delivery"]}},"source": "Counter Culture Coffee"},{"position": 7,"title": "Coffee | The Nutrition Source - hsph.harvard.edu",...},{"position": 8,"title": "Blanchard's Coffee Roasting Company",..."sitelinks": {"inline": [{"title": "Products","link": "https://blanchardscoffee.com/collections/all"},{"title": "Decent Instant Coffee","link": "https://blanchardscoffee.com/products/decent"},{"title": "Blanchard's Cafe Pickup","link": "https://pickup.blanchardscoffee.com/"},{"title": "Shop","link": "https://blanchardscoffee.com/pages/shop"}]},"rich_snippet": {"top": {"detected_extensions": {"price": 14,"currency": "$ to $","unknown": 5},"extensions": ["$14 to $15","\u200e 5","day delivery"]}},"source": "Blanchard's Coffee"},{"position": 9,"title": "The Coffee Bean & Tea Leaf | CBTL","link": "https://www.coffeebean.com/","redirect_link": "https://www.google.comhttps://www.coffeebean.com/","displayed_link": "https://www.coffeebean.com","favicon": "https://serpapi.com/searches/6602701cfe41d084fd3ed5da/images/19875953fd2763176ecb268a71b09fd9f88be5e04c633b3b580c37b1b81751ac.png","snippet": "Born and brewed in Southern California since 1963, The Coffee Bean & Tea Leaf\u00ae is passionate about connecting loyal customers with carefully handcrafted ...","snippet_highlighted_words": ["Coffee"],"source": "The Coffee Bean & Tea Leaf"}],"related_searches": [{"block_position": 1,"query": "Coffee shops near Texas","image": "https://lh5.googleusercontent.com/p/AF1QipOYChzmH_DA-1Yz3mF8lQyEcinfGtJzaiUBxyUP=w40-h40-n-k-no"},{"block_position": 2,"query": "Great coffee shop near Texas","image": "https://lh5.googleusercontent.com/p/AF1QipOYChzmH_DA-1Yz3mF8lQyEcinfGtJzaiUBxyUP=w40-h40-n-k-no"},...{"block_position": 1,"query": "coffee powder",...}],"refine_this_search": [{"query": "Whole Bean","link": "https://www.google.com/search?sca_esv=6cb148437a7a9a38&gl=us&hl=en&q=whole+bean+coffee&sa=X&ved=2ahUKEwizk5Ccq5GFAxUDjokEHeceA94Qs9oBKAB6BAguEAE","serpapi_link": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=Austin%2C+Texas&q=whole+bean+coffee","thumbnail": "https://encrypted-tbn2.gstatic.com/shopping?q=tbn:ANd9GcTJVcQZ1jGbu7SLjXBxneWc_xYUvdxhPAI1Gflp9AMbK4w7HS5fw3ItzDKnAx5qEle9SIbzx4FE9CZVnBoJDcN1jdTfbWEM50gozzPkGxbTQJ8236a419jb"},...{"query": "Bag",...},{"query": "Box",...}],"pagination": {"current": 1,"next": "https://www.google.com/search?q=Coffee&oq=Coffee&hl=en&gl=us&start=10&sourceid=chrome&ie=UTF-8","other_pages": {"2": "https://www.google.com/search?q=Coffee&oq=Coffee&hl=en&gl=us&start=10&sourceid=chrome&ie=UTF-8",..."5": "https://www.google.com/search?q=Coffee&oq=Coffee&hl=en&gl=us&start=40&sourceid=chrome&ie=UTF-8"}},"serpapi_pagination": {"current": 1,"next_link": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=Austin%2C+Texas&q=Coffee&start=10","next": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=Austin%2C+Texas&q=Coffee&start=10","other_pages": {"2": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=Austin%2C+Texas&q=Coffee&start=10",..."5": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=Austin%2C+Texas&q=Coffee&start=40"}}
}
The SerpApi.com API Documentation contains a list of all the possible parameters that can be passed to the API.
Basic Examples in Python
Search Bing
import os
import serpapiclient = serpapi.Client(api_key=os.getenv("API_KEY"))results = client.search({'engine': 'bing','q': 'coffee',
})
- API Documentation: serpapi.com/bing-search-api
Search Baidu
results = client.search({'engine': 'baidu','q': 'coffee',
})
- API Documentation: serpapi.com/baidu-search-api
Search Yahoo
results = client.search({'engine': 'yahoo','p': 'coffee',
})
- API Documentation: serpapi.com/yahoo-search-api
Search YouTube
results = client.search({'engine': 'youtube','search_query': 'coffee',
})
- API Documentation: serpapi.com/youtube-search-api
Search Walmart
results = client.search({'engine': 'walmart','query': 'coffee',
})
- API Documentation: serpapi.com/walmart-search-api
Search eBay
results = client.search({'engine': 'ebay','_nkw': 'coffee',
})
- API Documentation: serpapi.com/ebay-search-api
Search Naver
results = client.search({'engine': 'naver','query': 'coffee',
})
- API Documentation: serpapi.com/naver-search-api
Search Home Depot
results = client.search({'engine': 'home_depot','q': 'table',
})
- API Documentation: serpapi.com/home-depot-search-api
Search Apple App Store
results = client.search({'engine': 'apple_app_store','term': 'coffee',
})
- API Documentation: serpapi.com/apple-app-store
Search DuckDuckGo
results = client.search({'engine': 'duckduckgo','q': 'coffee',
})
- API Documentation: serpapi.com/duckduckgo-search-api
Search Google
results = client.search({'engine': 'google','q': 'coffee','engine': 'google',
})
- API Documentation: serpapi.com/search-api
Search Google Scholar
results = client.search({'engine': 'google_scholar','q': 'coffee',
})
- API Documentation: serpapi.com/google-scholar-api
Search Google Autocomplete
results = client.search({'engine': 'google_autocomplete','q': 'coffee',
})
- API Documentation: serpapi.com/google-autocomplete-api
Search Google Product
results = client.search({'engine': 'google_product','q': 'coffee','product_id': '4887235756540435899',
})
- API Documentation: serpapi.com/google-product-api
Search Google Reverse Image
results = client.search({'engine': 'google_reverse_image','image_url': 'https://i.imgur.com/5bGzZi7.jpg','max_results': '1',
})
- API Documentation: serpapi.com/google-reverse-image
Search Google Events
results = client.search({'engine': 'google_events','q': 'coffee',
})
- API Documentation: serpapi.com/google-events-api
Search Google Local Services
results = client.search({'engine': 'google_local_services','q': 'electrician','data_cid': '6745062158417646970',
})
- API Documentation: serpapi.com/google-local-services-api
Search Google Maps
results = client.search({'engine': 'google_maps','q': 'pizza','ll': '@40.7455096,-74.0083012,15.1z','type': 'search',
})
- API Documentation: serpapi.com/google-maps-api
Search Google Jobs
results = client.search({'engine': 'google_jobs','q': 'coffee',
})
- API Documentation: serpapi.com/google-jobs-api
Search Google Play
results = client.search({'engine': 'google_play','q': 'kite','store': 'apps','max_results': '2',
})
- API Documentation: serpapi.com/google-play-api
Search Google Images
results = client.search({'engine': 'google_images','tbm': 'isch','q': 'coffee',
})
- API Documentation: serpapi.com/images-results
2024-03-26(二)
头晕