亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python/C# 的動態 Google 搜索

使用 Python/C# 的動態 Google 搜索

慕容3067478 2023-03-08 15:26:11
我想檢索 Google 搜索結果計數( 106,000,000 個結果(0.58 秒))。我用 Python 寫了這個腳本:import requests, webbrowserfrom bs4 import BeautifulSoupuser_input = input("Type in query: ")print("Googling..")link = "http://www.google.com/search?q=" + user_inputgoogle_search = requests.get(link)print(google_search.headers)#print it out as filewith open("Output.html", "w") as text_file:    print("{}".format(google_search.text), file=text_file)但是當我查看文件時,結果統計信息就丟失了。除了 Google Search API 之外,還有什么方法可以做到這一點,這很糟糕,因為它是有限的,甚至無法獲得正確的結果。我寫過 Python 和 C#,因為我兩者都懂。
查看完整描述

2 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

要從 Google 獲得正確的結果,您必須設置正確的User-Agenthttp 標頭:


import requests

from bs4 import BeautifulSoup



user_input = input("Type in query: ")

print("Googling for keyword={}..".format(user_input))


params = {

    'q': user_input,

    'hl': 'en'   # <-- set hl=en to obtain english only results.

}

headers = {

    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'

}


google_search = requests.get("https://www.google.com/search", params=params, headers=headers)

soup = BeautifulSoup(google_search.content, 'html.parser')

print(soup.select_one('#result-stats').text)

打?。ɡ纾?/p>


Type in query: moon

Googling for keyword=moon..

About 1,720,000,000 results (0.99 seconds) 


查看完整回答
反對 回復 2023-03-08
?
DIEA

TA貢獻1820條經驗 獲得超3個贊

查看SelectorGadgetCSS Chrome 擴展程序,通過在瀏覽器中單擊所需的元素來獲取選擇器。css或者,如果您不喜歡通過命令在開發工具控制臺中進行選擇器,則可以使用它來測試選擇器$$('SELECTOR')。

使用css選擇器更靈活,更易讀,嘗試使用select_one()orselect() bs4方法而不是find()findAll()。CSS選擇器參考。

params此外,您可以像這樣傳遞 URL 查詢:

params = {

  'q': 'the most amazing query in 2021',

  'gl': 'hl',

}


requests.get(YOUR_URL, params=params)

代碼:


from bs4 import BeautifulSoup

import requests, lxml


headers = {

    'User-agent':

    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"

}


user_input = input("Type in query: ")

print(f"Googling... {user_input}")


params = {

  'q': user_input,

  'gl': 'hl',

}


soup = BeautifulSoup(requests.get('https://www.google.com/search', headers=headers, params=params).text, 'lxml')


print(f"Found {soup.select_one('#result-stats').text}"

      .replace("About", "about")

      .replace(" (", " in ")

      .replace(")", ""))


---------

'''

Type in query: fus ro dah

Googling... fus ro dah

Found about 628,000 results in 0.36 seconds 

'''

或者,您可以使用來自 SerpApi 的Google Organic Results API來實現相同的目的。這是一個帶有免費計劃的付費 API。

您的特定示例的主要區別在于您不需要弄清楚為什么某些事情沒有按預期工作,因為它已經為最終用戶完成了。在這種情況下唯一應該做的就是從結構化的 JSON 字符串中獲取所需的數據。

集成代碼:

from serpapi import GoogleSearch

import os


user_input = input("Type in query: ")

print(f"Googling... {user_input}")


params = {

  "api_key": os.getenv("API_KEY"),

  "engine": "google",

  "q": user_input,

  "hl": "en"

}


search = GoogleSearch(params)

results = search.get_dict()


print(f"Total results: {results['search_information']['total_results']}\n"

      f"Time taken: {results['search_information']['time_taken_displayed']}")


-------

'''

Type in query: fus ro dah

Googling... fus ro dah

Total results: 663000

Time took: 0.59 sec

'''

免責聲明,我為 SerpApi 工作。


查看完整回答
反對 回復 2023-03-08
  • 2 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號