Check whether URL access is normal

Source: Internet
Author: User
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)

:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.