
ChatGPT生態系統的安全漏洞導致第三方網站賬戶和敏感數據泄露
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)
谷歌和大多數網站一樣,并不太喜歡自動化程序獲取搜索結果頁面。
一個解決方案是通過設置正常的標題來掩蓋我們是自動化程序的事實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)
想要從頁面中提取實際的搜索結果。先要弄清楚如何訪問搜索結果,啟動了 Chrome 并檢查了 Google 搜索結果頁面:
我們可以使用這些信息通過 BeautifulSoup 提取搜索結果。
# 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")
當我檢查頁面時,我發現搜索標題包含在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 很快就會發現這是一個機器人并做出 IP攔截 。
方案一:以非常稀疏的方式進行抓取,并在每次請求之間等待 10 秒。但是,如果您需要抓取大量搜索查詢,那么這不是最佳解決方案。
方案二:另一個解決方案是購買 IP代理服務器。這樣你就可以從不同的 IP 地址抓取數據。但這里又有一個問題。很多人想抓取 Google 搜索結果,因此大多數代理已被 Google 屏蔽。
方案三:再一種方法是購買住宅IP代理,這些 IP 地址與真實用戶無法區分。
用冪簡集成搜索API最方便!冪簡集成專注于為開發者提供全面、高效、易用的API平臺解決方案。冪簡API平臺提供了多種維度發現API的功能:通過關鍵詞搜索API、從API Hub分類瀏覽API、從開放平臺分類瀏覽企業間接尋找API等。
此外,冪簡集成開發者社區會編寫API入門指南、多語言API對接指南、API測評等維度的文章,讓開發者選擇符合自己需求的API。