日誌2021-07-22 17:59
Line Bot研究 - 運用google-search-results套件實現自動抓圖片以及youtube網址 更新yt網址新方法作者:熾炎之翼
pip install google-search-results |
from serpapi import GoogleSearch search = GoogleSearch({"q": "coffee", "location": "Austin,Texas", "api_key": "secretKey"}) result = search.get_dict() |
{'search_metadata': {'id': '60f92e565fc493e862813241', 'status': 'Success', 'json_endpoint': 'https://serpapi.com/searches/ca84ecb880df1c2c/60f92e565fc493e862813241.json', 'created_at': '2021-07-22 08:37:42 UTC', 'processed_at': '2021-07-22 08:37:42 UTC', 'google_url': 'https://www.google.com/search?q=coffee&oq=coffee&sourceid=chrome&ie=UTF-8', 'raw_html_file': 'https://serpapi.com/searches/ca84ecb880df1c2c/60f92e565fc493e862813241.html', 'total_time_taken': 2.22}, 'search_parameters': {'engine': 'google', 'q': 'coffee', 'google_domain': 'google.com', 'device': 'desktop'}, 'search_information': {'organic_results_state': 'Results for exact spelling', 'total_results': 2420000000, 'time_taken_displayed': 1.04, 'query_displayed': 'coffee'}, 'local_map': {'link': 'https://www.google.com/search?q=coffee&npsic=0&rflfq=1&rldoc=1&rllag=-789449,113921330,19&tbm=lcl&sa=X&ved=2ahUKEwj0rfWao_bxAhXFHM0KHWbcA5MQtgN6BAgOEAQ', 'image': 'https://serpapi.com/searches/60f92e565fc493e862813241/images/138160cf56e903e0526b91de747b7147.png', 'gps_coordinates': {'latitude': -7.89449, 'longitude': 113.92133, 'altitude': 19}}, 'local_results': {'more_locations_link': 'https://www.google.com/search?tbs=lf:1,lf_ui:9&tbm=lcl&q=coffee&rflfq=1&num=10&sa=X&ved=2ahUKEwj0rfWao_bxAhXFHM0KHWbcA5MQjGp6BAgOEEQ', 'places': [{'position': 1, 'title': 'RASAHARSA COFEE SHOP', 'place_id': '7074646508022715519', 'lsig': 'AB86z5Vz9EVDe6jWlAXws7sRWJoH', 'place_id_search': 'https://serpapi.com/search.json?device=desktop&engine=google&google_domain=google.com&lsig=AB86z5Vz9EVDe6jWlAXws7sRWJoH&ludocid=7074646508022715519&q=coffee&tbm=lcl', 'type': 'Kedai Kopi', 'address': 'Jl. Rawamangun Muka Timur No.60, RT.3/RW.12, Rawamangun, Kec. Pulo Gadung, Kota Jakarta Timur, Daerah Khusus Ibukota Jakarta', 'thumbnail': 'https://serpapi.com/searches/60f92e565fc493e862813241/images/f843ddced0d0e6cb6390cab82d6108eb7bf1f0c6177e99d35f8c4af75440c9817a3da0f1f5e7f992.png', 'gps_coordinates': {'latitude': -0.7896234, 'longitude': 113.92133}}, {'position': 2, 'title': 'Teras Tongkrong', 'place_id': '9352390002825474361', 'lsig': 'AB86z5WVeaK9CJMwcCUFrdnOwtY_', 'place_id_search': 'https://serpapi.com/search.json?device=desktop&engine=google&google_domain=google.com&lsig=AB86z5WVeaK9CJMwcCUFrdnOwtY_&ludocid=9352390002825474361&q=coffee&tbm=lcl', 'rating': 4.1, 'reviews': 14, 'type': 'Kafe', 'address': 'Makan di tempat · Bawa pulang', 'thumbnail': 'https://serpapi.com/searches/60f92e565fc493e862813241/images/f843ddced0d0e6cb6390cab82d6108eb9abd2d91a1428592ef4c4094e354286bab2242420823eb22.jpeg', 'gps_coordinates': {'latitude': -0.789275, 'longitude': 113.921326}} |
if get_message[-4:].lower() == '.jpg': URL_list = [] try: params = { "engine": "google", "tbm": "isch", "api_key": "Your KEY", } params['q'] = get_message client = GoogleSearch(params) data = client.get_dict() imgs = data['images_results'] x = 0 for img in imgs: if x < 5: URL_list.append(img['original']) x += 1 except: url = 'https://www.google.com.tw/search?q=' + \ get_message+'&tbm=isch' request = requests.get(url=url) html = request.content bsObj = BeautifulSoup(html, 'html.parser') content = bsObj.findAll('img', {'class': 't0fcAb'}) for i in content: URL_list.append(i['src']) url = random.choice(URL_list) message = ImageSendMessage( original_content_url=url, preview_image_url=url ) line_bot_api.reply_message(event.reply_token, message) |
get_message = event.message.text.rstrip() |
if get_message[:3].lower() == '!yt': URL_list = [] params = { "engine": "google", "tbm": "vid", "api_key": "Your_KEY", } params['q'] = get_message[4:] client = GoogleSearch(params) data = client.get_dict() imgs = data['video_results'] x = 0 for img in imgs: if x < 1 and 'youtube' in img['link']: URL_list.append(img['link']) x += 1 reply = TextSendMessage(text=URL_list[0]) line_bot_api.reply_message(event.reply_token, reply) |
if get_message[:3].lower() == '!yt': YOUTUBE_API_KEY = jdata['YOUTUBE_API_KEY'] q = get_message[4:] url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + \ q+'&key='+YOUTUBE_API_KEY+'&type=video&maxResults=1' request = requests.get(url) data = request.json() URL = 'https://www.youtube.com/watch?v=' + \ data['items'][0]['id']['videoId'] reply = TextSendMessage(text=URL) line_bot_api.reply_message(event.reply_token, reply) |
{\n "kind": "youtube#searchListResponse",\n "etag": "2CTo31NZZ8LEp4XhzJh8e6anA78",\n "nextPageToken": "CBQQAA",\n "regionCode": "TW",\n "pageInfo": {\n "totalResults": 1000000,\n "resultsPerPage": 20\n },\n "items": [\n {\n "kind": "youtube#searchResult",\n "etag": "nkf-tdZqiRsIyZpYUJxZDHfCNxc",\n "id": {\n "kind": "youtube#video",\n "videoId": "k2OX38jCfgg"\n },\n "snippet": {\n "publishedAt": "2021-07-01T14:00:09Z",\n "channelId": "UCgdwtyqBunlRb-i-7PnCssQ",\n "title": "Love Live! \xe4\xb8\x80\xe6\x9c\x9f \xe7\xac\xac01\xe8\xa9\xb1\xe3\x80\x90\xe5\xaf\xa6\xe7\x8f\xbe\xe5\x90\xa7!\xe6\x88\x91\xe5\x80\x91\xe7\x9a\x84\xe5\xa4\xa2\xe6\x83\xb3\xe3\x80\x91\xef\xbd\x9cMuse\xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1 \xe5\x8b\x95\xe7\x95\xab \xe7\xb7\x9a\xe4\xb8\x8a\xe7\x9c\x8b",\n "description": "\xe3\x80\x8aLove Live!\xe3\x80\x8b 7/01(\xe5\x9b\x9b)\xe8\xb5\xb7\xef\xbc\x8c\xe6\xaf\x8f\xe5\xa4\xa9\xe6\x99\x9a\xe4\xb8\x8a10\xef\xbc\x9a00\xe6\x9b\xb4\xe6\x96\xb0 \xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1YouTube\xe9\xa0\xbb\xe9\x81\x93\xe9\xa6\x96\xe6\x92\xad \xe3\x80\x90MUSE\xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1\xe6\xa8\x82\xe5\x9c\x92\xe3\x80\x91\xe2\x9c\xa8\xe5\x85\xa8\xe6\x96\xb0\xe8\xb3\xbc\xe7\x89\xa9\xe5\xae\x98\xe7\xb6\xb2/APP\xe6\xad\xa3\xe5\xbc\x8f\xe5\x95\x9f\xe7\x94\xa8\xe5\x85\x8d\xe5\x87\xba\xe9\x96\x80\xe5\x8b\x95\xe6\xbc\xab ...",\n "thumbnails": {\n "default": {\n "url": "https://i.ytimg.com/vi/k2OX38jCfgg/default.jpg",\n "width": 120,\n "height": 90\n },\n "medium": {\n "url": "https://i.ytimg.com/vi/k2OX38jCfgg/mqdefault.jpg",\n "width": 320,\n "height": 180\n },\n "high": {\n "url": "https://i.ytimg.com/vi/k2OX38jCfgg/hqdefault.jpg",\n "width": 480,\n "height": 360\n }\n },\n "channelTitle": "Muse\xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1-TW",\n "liveBroadcastContent": "none",\n "publishTime": "2021-07-01T14:00:09Z"\n }\n },\n {\n "kind": "youtube#searchResult",\n "etag": "-f1aWepXgJq8mfd9idUvOlopIVw",\n "id": {\n "kind": "youtube#video",\n "videoId": "3Ix2gzd0kAc"\n },\n "snippet": {\n "publishedAt": "2021-07-19T14:00:11Z",\n "channelId": "UCgdwtyqBunlRb-i-7PnCssQ",\n "title": "Love Live! \xe4\xba\x8c\xe6\x9c\x9f \xe7\xac\xac06\xe8\xa9\xb1\xe3\x80\x90\xe8\x90\xac\xe8\x81\x96\xe7\xaf\x80\xe5\xbf\xab\xe6\xa8\x82\xe3\x80\x91\xef\xbd\x9cMuse\xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1 \xe5\x8b\x95\xe7\x95\xab \xe7\xb7\x9a\xe4\xb8\x8a\xe7\x9c\x8b",\n "description": "\xe3\x80\x8aLove Live!\xe3\x80\x8b 7/01(\xe5\x9b\x9b)\xe8\xb5\xb7\xef\xbc\x8c\xe6\xaf\x8f\xe5\xa4\xa9\xe6\x99\x9a\xe4\xb8\x8a10\xef\xbc\x9a00\xe6\x9b\xb4\xe6\x96\xb0 \xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1YouTube\xe9\xa0\xbb\xe9\x81\x93\xe9\xa6\x96\xe6\x92\xad \xe3\x80\x90MUSE\xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1\xe6\xa8\x82\xe5\x9c\x92\xe3\x80\x91\xe2\x9c\xa8\xe5\x85\xa8\xe6\x96\xb0\xe8\xb3\xbc\xe7\x89\xa9\xe5\xae\x98\xe7\xb6\xb2/APP\xe6\xad\xa3\xe5\xbc\x8f\xe5\x95\x9f\xe7\x94\xa8\xe5\x85\x8d\xe5\x87\xba\xe9\x96\x80\xe5\x8b\x95\xe6\xbc\xab ...",\n "thumbnails": {\n "default": {\n "url": "https://i.ytimg.com/vi/3Ix2gzd0kAc/default.jpg",\n "width": 120,\n "height": 90\n },\n "medium": {\n "url": "https://i.ytimg.com/vi/3Ix2gzd0kAc/mqdefault.jpg",\n "width": 320,\n "height": 180\n },\n "high": {\n "url": "https://i.ytimg.com/vi/3Ix2gzd0kAc/hqdefault.jpg",\n "width": 480,\n "height": 360\n }\n },\n "channelTitle": "Muse\xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1-TW",\n "liveBroadcastContent": "none",\n "publishTime": "2021-07-19T14:00:11Z"\n }\n },\n {\n "kind": "youtube#searchResult",\n "etag": "nwvzRowcQj9lwLSISwoAofvjvio",\n "id": {\n "kind": "youtube#video",\n "videoId": "DobT0FEyZDA"\n },\n "snippet": {\n "publishedAt": "2021-07-18T14:00:10Z",\n "channelId": "UCgdwtyqBunlRb-i-7PnCssQ",\n "title": "Love Live! \xe4\xba\x8c\xe6\x9c\x9f \xe7\xac\xac05\xe8\xa9\xb1\xe3\x80\x90\xe5\x85\xa8\xe6\x96\xb0\xe7\x9a\x84\xe6\x88\x91\xe3\x80\x91\xef\xbd\x9cMuse\xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1 \xe5\x8b\x95\xe7\x95\xab \xe7\xb7\x9a\xe4\xb8\x8a\xe7\x9c\x8b",\n "description": "\xe3\x80\x8aLove Live!\xe3\x80\x8b 7/01(\xe5\x9b\x9b)\xe8\xb5\xb7\xef\xbc\x8c\xe6\xaf\x8f\xe5\xa4\xa9\xe6\x99\x9a\xe4\xb8\x8a10\xef\xbc\x9a00\xe6\x9b\xb4\xe6\x96\xb0 \xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1YouTube\xe9\xa0\xbb\xe9\x81\x93\xe9\xa6\x96\xe6\x92\xad \xe3\x80\x90MUSE\xe6\x9c\xa8\xe6\xa3\x89\xe8\x8a\xb1\xe6\xa8\x82\xe5\x9c\x92\xe3\x80\x91\xe2\x9c\xa8\xe5\x85\xa8\xe6\x96\xb0\xe8\xb3\xbc\xe7\x89\xa9\xe5\xae\x98\xe7\xb6\xb2/APP\xe6\xad\xa3\xe5\xbc\x8f\xe5\x95\x9f\xe7\x94\xa8\xe5\x85\x8d\xe5\x87\xba\xe9\x96\x80\xe5\x8b\x95\xe6\xbc\xab ...",\n "thumbnails": {\n "default": {\n "url": "https://i.ytimg.com/vi/DobT0FEyZDA/default.jpg",\n "width": 120,\n "height": 90\n },\n "medium": {\n "url": "https://i.ytimg.com/vi/DobT0FEyZDA/mqdefault.jpg",\n |
2021-07-22 18:10熾炎之翼:因為他後面的伺服器可能用到JS或其他手法搞出網址
簡單來說就是他掌握大家沒有的技術
2021-07-22 18:13熾炎之翼:還有很大部分的進步空間呢
2021-07-22 18:13熾炎之翼:那其實算一個機構喔
所以其實也不是很意外就是了
2021-07-22 18:29熾炎之翼:沒
2021-07-22 18:29熾炎之翼:我自己是沒有很滿意目前的寫法啦…
感覺偏湊合
2021-07-22 18:30熾炎之翼:過獎ㄌ
2021-07-22 18:31熾炎之翼:小研究而已
2021-07-22 19:55熾炎之翼:鐘乳石.jpg
2021-07-22 20:50熾炎之翼:您合太電機 更強吧...
2021-07-22 22:58熾炎之翼:笑死 不過以反應速度來看 這個應該不可能 吧
2021-07-23 16:21熾炎之翼:酷酷酷
2021-07-24 21:12熾炎之翼:度的度度
2021-07-24 22:07熾炎之翼:沒有 我是抓下來轉成json