Python checks URLs for normal access

Source: Internet
Author: User

53008439

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

  1. Import Urllib.request
  2. Import Time
  3. Opener = Urllib.request.build_opener ()
  4. Opener.addheaders = [(' user-agent ', ' mozilla/49.0.2 ')]
  5. #这个是你放网址的文件名, just make it right.
  6. File = open (' test.txt ')
  7. lines = File.readlines ()
  8. Aa=[]
  9. For line in lines:
  10. Temp=line.replace (' \ n ',')
  11. Aa.append (temp)
  12. Print (AA)
  13. Print (' start check: ')
  14. For A in AA:
  15. Tempurl = A
  16. Try:
  17. Opener.open (Tempurl)
  18. Print (tempurl+' no problem ')
  19. except Urllib.error.HTTPError:
  20. Print (tempurl+' = Error accessing page ')
  21. Time.sleep (2)
  22. except Urllib.error.URLError:
  23. Print (tempurl+' = Error accessing page ')
  24. Time.sleep (2)
  25. Time.sleep (0.1)

Python checks URLs for normal access

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.