Two methods for obtaining webpage status codes in Python: python webpage

Source: Internet
Author: User

Two methods for obtaining webpage status codes in Python: python webpage

The first method is to use the urllib module. The following is the sample code:

Copy codeThe Code is as follows:
Import urllib
Status = urllib. urlopen ("http://www.bkjia.com"). code
Print status

The second chapter uses the requests module. The following is the sample code:
Copy codeThe Code is as follows:
Import requests
Code = requests. get ("http://www.bkjia.com"). status_code
Print code


How does one obtain content on a webpage in Python? I want to use python to read different words and the number of times each word appears on the webpage.

Hi!
First, the content displayed by the browser to the user is completely based on the html source code. Therefore, all the content displayed by the browser you want to obtain is the content that exists in the html file.
The words on the statistics page must be read from the html source file.
You can use the urllib2 Library and the re library for matching and search. The Code is as follows:
Import urllib2import reword = 'urlfile = urllib2.urlopen ('content of the URL you want to open, remember to bring the Protocol prefix, such as http ') # Open a webpage in the format of html = urlfile. read () # read htmlwordList = re from a webpage file. findall (re. compile (word), html) # regular findall search for all the words print len (wordList) # The number of re modules is hard to remember. If you haven't tried it, try it. If not, try again.


In python, how does one obtain the webpage source code with a non-200 return code?

Non-200 may not have source code. You can refer to RFC2616, for example, 302 and 301. Then you can request the Location url in the header, and you can give up 5xx ......
 

Related Article

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.