
ChatGPT生態(tài)系統(tǒng)的安全漏洞導致第三方網(wǎng)站賬戶和敏感數(shù)據(jù)泄露
import urllib.request
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
# Urlencode the URL
url = urllib.parse.quote_plus("https://www.google.com/search?q=用冪簡集成搜索API")
# Create the query URL.
query = "https://api.scraperbox.com/scrape"
query += "?api_key=%s" % "YOUR_API_KEY"
query += "&url=%s" % url
# Call the API.
request = urllib.request.Request(query)
raw_response = urllib.request.urlopen(request).read()
html = raw_response.decode("utf-8")
print(html)
谷歌和大多數(shù)網(wǎng)站一樣,并不太喜歡自動化程序獲取搜索結(jié)果頁面。
一個解決方案是通過設置正常的標題來掩蓋我們是自動化程序的事實User-Agent
。
...
request = urllib.request.Request(query)
# Set a normal User Agent header
request.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')
raw_response = urllib.request.urlopen(request).read()
# Read the repsonse as a utf-8 string
html = raw_response.decode("utf-8")
print(html)request = urllib.request.Request(query)
想要從頁面中提取實際的搜索結(jié)果。先要弄清楚如何訪問搜索結(jié)果,啟動了 Chrome 并檢查了 Google 搜索結(jié)果頁面:
我們可以使用這些信息通過 BeautifulSoup 提取搜索結(jié)果。
# Construct the soup object
soup = BeautifulSoup(html, 'html.parser')
# Find all the search result divs
divs = soup.select("#search div.g")
for div in divs:
# For now just print the text contents.
print(div.get_text() + "\n\n")
當我檢查頁面時,我發(fā)現(xiàn)搜索標題包含在h3
標簽中。我們可以利用這些信息來提取標題。
# Find all the search result divs
divs = soup.select("#search div.g")
for div in divs:
# Search for a h3 tag
results = div.select("h3")
# Check if we have found a result
if (len(results) >= 1):
# Print the title
h3 = results[0]
print(h3.get_text())</code></pre>
按此方式解析其它要素。
其它兩個步驟比較簡單,不再講解。
Google 很快就會發(fā)現(xiàn)這是一個機器人并做出 IP攔截 。
方案一:以非常稀疏的方式進行抓取,并在每次請求之間等待 10 秒。但是,如果您需要抓取大量搜索查詢,那么這不是最佳解決方案。
方案二:另一個解決方案是購買 IP代理服務器。這樣你就可以從不同的 IP 地址抓取數(shù)據(jù)。但這里又有一個問題。很多人想抓取 Google 搜索結(jié)果,因此大多數(shù)代理已被 Google 屏蔽。
方案三:再一種方法是購買住宅IP代理,這些 IP 地址與真實用戶無法區(qū)分。
用冪簡集成搜索API最方便!冪簡集成專注于為開發(fā)者提供全面、高效、易用的API平臺解決方案。冪簡API平臺提供了多種維度發(fā)現(xiàn)API的功能:通過關(guān)鍵詞搜索API、從API Hub分類瀏覽API、從開放平臺分類瀏覽企業(yè)間接尋找API等。
此外,冪簡集成開發(fā)者社區(qū)會編寫API入門指南、多語言API對接指南、API測評等維度的文章,讓開發(fā)者選擇符合自己需求的API。
ChatGPT生態(tài)系統(tǒng)的安全漏洞導致第三方網(wǎng)站賬戶和敏感數(shù)據(jù)泄露
想要系統(tǒng)了解Agentic Workflow,看這25篇論文就夠了
生成式 AI 在電商領域究竟有多牛,這款產(chǎn)品給出了回答
AI+搜索:在Elastic的推理API中嵌入大語言模型Cohere API
AI Agent 開源和創(chuàng)業(yè)項目大盤點,Agent 基礎設施正在崛起
人工智能(AI) VS 商業(yè)智能(BI) 區(qū)別與聯(lián)系是什么?
使用 RESTful API 探索藝術(shù)世界
如何在Java、Python、GO程序中使用AI人臉識別API接口
什么是API產(chǎn)品經(jīng)理?