[英]How to rotate proxy with Python
我正在嘗試使用 Python 並嘗試構建簡單的 Google Scraper。 為此,我嘗試實現代理,但我沒有足夠的知識。
到目前為止,我的代碼如下所示:
import requests
from bs4 import BeautifulSoup as bs
import warnings
from googlesearch import search
from itertools import cycle
warnings.filterwarnings("ignore", module='bs4')
proxies = {
'http:myproxies_url'
}
proxy_pool = cycle(proxies)
proxy = next(proxy_pool)
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive'}
def searchBing(query, num):
urls = []
for j in search(query, tld="co.in", num=10, stop=10, pause=3):
proxies = {
"http": proxy,
"https":proxy
}
urls.append(j)
return urls[:num]
def extractText(url):
page = requests.get(url,proxies=proxies)
soup = bs(page.text, 'html.parser')
return soup.get_text()
這是控制台的錯誤:
no_proxy = proxies.get('no_proxy') if proxies is not None else None
AttributeError: 'set' object has no attribute 'get'
proxies
是一個集合,
proxies = {
'http:myproxies_url'
}
該錯誤向我表明它沒有通過searchBing
函數設置為字典。 我建議更新您的變量名稱,以免發生這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.