python3.7 Simple Crawler Example Explanation

  • 2021-07-10 20:27:31
  • OfStack

python 3.7 Simple crawler, the specific code is as follows:


#https://www.runoob.com/w3cnote/python-spider-intro.html

#Python  Introduction to reptiles 

import urllib.parse
import urllib.request
from http import cookiejar
url = "http://www.baidu.com"
response1 = urllib.request.urlopen(url)
print(" No. 1 1 Methods ")
# Gets the status code, 200 Indicate success 
print(response1.getcode())
# Gets the length of the Web page content 
print(str(response1.read()))
print(len(response1.read()))
print(" No. 1 2 Methods ")
request = urllib.request.Request(url)
# Simulation Mozilla The browser crawls 
request.add_header("user-agent","Mozilla/5.0")
response2 = urllib.request.urlopen(request)
print(response2.getcode())
print(len(response2.read()))
print(" No. 1 3 Methods ")
cookie = cookiejar.CookieJar()
# Join urllib2 Deal with cookie The ability of #
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
urllib.request.install_opener(opener)
response3 = urllib.request.urlopen(url)
print(response3.getcode())
print(len(response3.read()))
print(cookie)

code :  https://github.com/pascal19821003/python
path :  python/study/tutorial/pachong/1.py

Summarize


Related articles: