问题:检查超过 1000 个 url 的列表并获取 url 返回码 (status_code)。
我的脚本有效,但速度很慢。
我认为必须有一种更好的、pythonic(更漂亮)的方式来做到这一点,在那里我可以产生 10 或 20 个线程来检查 url 并收集响应。(IE:
200 -> www.yahoo.com
404 -> www.badurl.com
...
Run Code Online (Sandbox Code Playgroud)
www.example.com
www.yahoo.com
www.testsite.com
Run Code Online (Sandbox Code Playgroud)
....
import requests
with open("url10.txt") as f:
urls = f.read().splitlines()
print(urls)
for url in urls:
url = 'http://'+url #Add http:// to each url (there has to be a better way to do this)
try:
resp = requests.get(url, timeout=1)
print(len(resp.content), '->', resp.status_code, '->', resp.url)
except Exception as e:
print("Error", url)
Run Code Online (Sandbox Code Playgroud)
挑战: 通过多处理提高速度。
但它不工作。我收到以下错误:(注意:我不确定我是否正确实现了这一点) …
python multithreading multiprocessing python-multiprocessing