It is easy to check whether a URL is normal. what should I do if I check 2000 URLs or a large number of URLs? This article introduces you to the tips for checking whether URLs are accessed normally in python. it has some reference value. if you are interested, you can refer to it to check whether a URL is normal, what if I check 2000 URLs or a large number of URLs? This article introduces you to the tips for checking whether URLs are accessible in python. it has some reference value. if you are interested, you can refer to it.
Today, the project manager asked me a question and asked me if there are 2000 URLs to check whether they can be opened normally. In fact, I refused. I know that it is necessary to write code, I just learned something about Python. I thought it would be easy to process python. I chose python and started to think about it:
1. the first 2000 URLs. It can be placed in a txt file.
2. put the URL in the content one by one into the array using python
3. open a simulated browser for access.
4. if the access is normal, the output is normal. if the access is normal, an error is returned.
The code is simply and roughly dumped. Because privacy is involved, the image is coded
Import urllib. request import time opener = urllib. request. build_opener () opener. addheaders = [('User-agent', 'mozilla/49.0.2 ')] # This is the file name of your website. you can change it to 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') failed t urllib. error. HTTPError: print (tempUrl + '= Page access error') time. sleep (2) failed t urllib. error. URLError: print (tempUrl + '= Page access error') time. sleep (2) time. sleep (0.1)
: