The solution of ‘STR’ object has no attribute ‘get’ error

I used the requests.get()method when writing a crawler in python :

DEF OpenURL (URL, IP, Agent):
     # function parameter is url: web page address; ip: ip pool; agent: User-Agent, three are string type 
    requests.get (url, headers = agent, proxies = ip)

What is puzzled is that the’str’ object has no attribute’get’ error was reported when using it

After reviewing the documentation, it was found that the headers and proxies parameters in the get() method should be passed in a dictionary instead of a string , so after modification, the code runs successfully:

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

 

Similar Posts: