Home > Backend Development > Python Tutorial > Python checks whether the URL is accessible normally

Python checks whether the URL is accessible normally

高洛峰
Release: 2017-02-28 09:15:16
Original
1404 people have browsed it

Today, the project manager asked me a question, asking me that there are 2,000 URLs here to check whether they can be opened normally. In fact, I refused. I knew that because I had to write code, I happened to learn some Python. When I thought about it, python It was easy to handle, so I chose python and started thinking about it:

1. First, 2000 URLs. It can be placed in a txt text
2. Put the URLs in the content into the array one by one through python
3. Open a simulated browser and access it.
4. If the access is normal, the output will be normal, and if there is an error, the error will be output.

Just throw away the code simply and crudely. Because it involves privacy, the picture has been coded

import urllib.request 
import time 
 
opener = urllib.request.build_opener() 
opener.addheaders = [('User-agent', 'Mozilla/49.0.2')] 
#这个是你放网址的文件名,改过来就可以了 
file = open('test.txt') 
lines = file.readlines() 
aa=[] 
for line in lines: 
  temp=line.replace('\n','') 
  aa.append(temp) 
print(aa) 
 
print('开始检查:') 
for a in aa: 
  tempUrl = a 
  try : 
    opener.open(tempUrl) 
    print(tempUrl+'没问题') 
  except urllib.error.HTTPError: 
    print(tempUrl+'=访问页面出错') 
    time.sleep(2) 
  except urllib.error.URLError: 
    print(tempUrl+'=访问页面出错') 
    time.sleep(2) 
  time.sleep(0.1)
Copy after login

Rendering:

Python checks whether the URL is accessible normally

The above is the content of this article All the content, I hope it will be helpful to everyone's learning, and I also hope everyone will support the PHP Chinese website.

For more articles related to python checking whether the URL is accessible normally, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template