dimanche 31 mai 2020

How to use dict to store and use if-statement [closed]

So I have been trying to figure out on how to store and how to see if a value is inside a dict.

What I'm trying to do is to save headers and cookies when doing a requests to save it and to re-use it.

What I have done is:

while True:

    dediProxies = random.randint(0,9)

    proxiesDict = {}

    try:

        if dediProxies in proxiesDict:
            scraper.headers = proxiesDict[dediProxies]['headers']
            scraper.cookies = proxiesDict[dediProxies]['cookies']

        response = requests.get('https://www.helloworld.com')

        if dediProxies not in proxiesDict:
            proxiesDict[dediProxies] = {
                'headers': response.headers,
                'cookies': response.cookies
            }

    except Exception as err:
        print(err)
        sys.exit()

However unfortunately my problem is that I am getting the error:

Traceback (most recent call last):
  File "C:/Users/test.py", line 10, in <module>
    if dediProxies in proxiesList:
TypeError: unhashable type: 'dict'

My question is, How can I have a dict storage where I can save the headers and cookies to re-use - if we have matched if statement, re-use the headers and cookies and then continue and IF there are not in the DICT then we add it after the requests is done?

Aucun commentaire:

Enregistrer un commentaire