'str' object has no attribute 'get' 錯誤解決方案


我在使用python寫爬蟲時用到了requests.get()方法:

def openUrl(url, ip, agent):
    #函數形參為url:網頁地址; ip:ip池; agent:User-Agent, 三者均為字符串類型
    requests.get(url, headers=agent, proxies=ip)

疑惑的是,使用時報了 ‘str’ object has no attribute ‘get’ 錯誤

查看文檔后發現,是由於get()方法中的headers和proxies參數應傳入字典而不是字符串,於是經修改,代碼成功運行:

def openUrl(url, ip, agent):
    headers = {'User-Agent': agent}
    proxies = {'http' : ip}
    requests.get(url, headers=headers, proxies=proxies)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM