I’m a novice python programmer and I’m trying to use the requests library to find http status codes for a large list of urls, put those status codes into their own array, then add the status code array back to the dataframe as a new column.
Here’s a very basic version of the code that I’m using.
import requests
import pandas as pd
targets =pd.read_csv('/file/path.csv',header=None)
targetList =targets.values
for i in targetList:
r = requests.get (f"{i}")
r.status_code
I’m not concerned about the dataframe manipulation, that seems simple enough. And I can get the requests to work as discrete incidents
r=requests.get(targetList.item(0))
code=r.status_code
code
200
When I try to run the for loop however I get the following error.
InvalidSchema: No connection adapaters were found for "['https://www.google.com']"
Clearly the program is at least getting far enough to understand that the items in the list are strings, and understands the contents of those strings. But there’s a disconnect happening that I don’t understand.
2
Answers
Use:
The following code reproduces your error for me:
and the following returns 200:
variable
i
gives you list with all values in row – even if you have only one column – and you have to get single value from this list – ie.i[0]
Result:
Or you should select single column –
df['urls']
Result: