with open('IP.txt', 'r', encoding="utf-8") as f:#打開IP的地址 讀取 txt = f.read() return random.choice(txt.split('\n')) #反饋合成列表 隨即選擇一個 ’'' 例如: IP.txt 里的內容 隨即提取每一行 182.92.242.11:80 58.52.201.117:8080 175.43.156.60:9999 47.110.59.21:8080 175.43.34.49:9999 1.198.72.164:9999 222.190.110.167:808 使用txt.split('\n') 后成這樣 ['182.92.242.11:80', '58.52.201.117:8080', '175.43.156.60:9999', '47.110.59.21:8080', '175.43.34.49:9999', '1.198.72.164:9999', '222.190.110.167:808'] '''
接着用法:
url = 'http://www.dmoz.org/Computers/Programming/Languages/Python/Books/' filename = url.split("/")[-2] print(filename) ''' Books 以/為分界線把url分割成列表,-2取倒數第二個,分割網站 提取名字很好用。 '''