Is it easy to check if a URL is normal, if you check 2000 URLs, or a large number of URLs? This article for you to introduce the Python check URL is a normal access to the tips, with a certain reference value, interested in small partners can refer to
Today, the project manager asked me a question, asked me here are 2000 URLs to check whether it can open, in fact, I was refused, I know because to write code, just learned a little python, a think, Python processing easy, chose Python, began to think good:
1. First 2000 URLs. Can be placed within a txt text
2. Use Python to put the URL within the content into the array in one line
3. Open a simulated browser to access it.
4. If normal access is output normal, error output error
Direct simple brute-dump code. Because of the privacy involved, the picture was coded
Import urllib.request Import time opener = Urllib.request.build_opener () opener.addheaders = [(' User-agent ', ' mozilla/49.0.2 ')] #这个是你放网址的文件名, you can do it. File = open (' test.txt ') lines = File.readlines () aa=[] for line in lines: temp= Line.replace (' \ n ', ') aa.append (temp) print (AA) print (' Start check: ') for a in AA: tempurl = a try: Opener.open (Tempurl) print (tempurl+ ' no problem ') except Urllib.error.HTTPError: print (tempurl+ ' = Error accessing page ') Time.sleep (2) except Urllib.error.URLError: print (tempurl+ ' = Error accessing page ') time.sleep (2) Time.sleep (0.1)
:
"Recommended"
1. Python Free video tutorial
2. Python Basics Getting Started tutorial
3. Python Object-oriented video tutorial