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 ......