Requests Library exercise

Example one: Jingdong item page crawling
Import Requests
URL = "http://item.jd.com/2967929.html"
the try:
R & lt requests.get = (URL)
r.raise_for_status ()
r.encoding = r.apparent_encoding
Print (r.text [: 1000])
the except:
Print ( "crawling failure")
example 2: Amazon item page crawling
Import Requests
URL = "https://www.amazon.cn/gp/product/B01M8L5Z3Y"
the try:
= {kV 'user_agent': 'the Mozilla / 5.0'}
R & lt requests.get = (URL, headers = kV)
r.raise_for_status ()
R & lt, encoding = r.apparent_encoding
Print (r.text [1000: 2000])
the except:
print ( "crawling failure")
example III: Submit Baidu / search key 360
Import Requests
keyword = "Python"
try:
kv = {'q':keyword}
r = requests.get("http://www.so.com/s",params = kv)
print(r.request.url)
r.raise_for_status()
print(len(r.text))
except:
print("爬取失败")
实例四:网络图片的爬取和存储
import requests
import os
url = ""
root = "D://pics//"
path = root + url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(url)
with open(path,'wb') as f :
f.write(r.content)
f.close()
print("sucess")
else:
print("cunzai")
the except:
Print ( "crawling failure")
Example five: IP address of the home automatic query
Import Requests
URL = "http://m.ip138.com/ip.asp?ip="
the try:
R & lt requests.get = ( + URL '202.204.80.112')
r.raise_for_status ()
r.encoding = r.apparent_encoding
Print (r.text [-500:])
the except:
Print ( "crawling failure")

Guess you like

Origin www.cnblogs.com/lskai/p/11910912.html