所以我正在使用 pandas 和請求從https://free-proxy-list.net/抓取 IP,但我如何覆寫這段代碼
import pandas as pd
resp = requests.get('https://free-proxy-list.net/')
df = pd.read_html(resp.text)[0]
df = (df[(df['Anonymity'] == 'elite proxy')])
print(df.to_string(index=False))
因此輸出是IP串列,沒有其他任何內容。我設法洗掉了索引并只添加了精英代理,但我無法創建一個只有 IP 且沒有索引的串列的變數。
uj5u.com熱心網友回復:
要獲取“IP 地址”列的內容,請子集到“IP 地址”列并使用.to_list().
這是如何做:
print(df['IP Address'].to_list())
uj5u.com熱心網友回復:
您可以使用loc直接對匹配行的列進行切片,并to_list轉換為串列:
df.loc[df['Anonymity'].eq('elite proxy'), 'IP Address'].to_list()
輸出:['134.119.xxx.xxx', '173.249.xxx.xxx'...]
uj5u.com熱心網友回復:
看起來您正在嘗試完成以下操作:
print(df['IP Address'].to_string(index=False))
此外,在過濾資料框以重置其索引后,這將是一個好主意,如下所示:
df = df.reset_index(drop=True)
所以代碼片段會是這樣的:
import pandas as pd
resp = requests.get('https://free-proxy-list.net/')
df = pd.read_html(resp.text)[0]
df = (df[(df['Anonymity'] == 'elite proxy')])
df = df.reset_index(drop=True)
print(df['IP Address'].to_string(index=False))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/429577.html
標籤:Python python-3.x 熊猫
上一篇:如何使用python資料框查找具有相同識別符號的兩行中的值的比率
下一篇:如何長時間檢測時間序列中的缺失值
