requests 是对 urlopen 的封装
常用方法如下:import requests
url = 'http://www.baidu.com'
# get和pose请求
response = requests.get(url)
# 请求成功
print(response)
# 获取网页文本内容
print(response.text)
# reason 原因
# 请求状态的说明
print(response.reason)
# link 跳转的地址
print(response.links)
# 请求历史
print(response.history)
# 请求编码格式
print(response.apparent_encoding)
# 设置响应的编码格式为网页的编码格式
response.encoding = response.apparent_encoding
# 获取网页内容(bytes形式)
print(response.content)
# 获取网页cookie
print(response.cookies)
# 获取响应头信息
print(response.headers)
# 获取请求的网址
print(response.request.url)
以获取城市天气为例:
import requests
import json
from prettyprinter import pprint
class Weather(object):
def __init__(self):
# 获取本地信息
self.location_url = 'http://api.map.baidu.com/location/ip?&ak=KQvhEkxc6mFCAYHTblC3NGVmxzxIWk0E&coor=bd09ll'
# 获取天气信息
self.weather_url = 'http://api.map.baidu.com/telematics/v3/weather?output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?&location='
def start_spider(self):
cityName =self.get_location()
self.get_Weather_info(cityName)
while True:
cityName = input('请输入城市名字')
if cityName == 'E':
return
self.get_Weather_info(cityName)
def get_location(self):
response = requests.get(self.location_url)
print(response)
# 获取网页的内容
print(response.content)
# 将内容转化为字典对象
result_dic = json.loads(response.content)
pprint(result_dic)
city = result_dic['content']['address_detail']['city']
return city
def get_Weather_info(self,cityName):
url = self.weather_url + cityName
response = requests.get(url)
weather_dic = json.loads(response.content)
pprint(weather_dic)
for dayDic in weather_dic['results'][0]['weather_data']:
print('{}'.format(dayDic['date']))
print('温度:{}'.format(dayDic['temperature']))
w= Weather()
w.start_spider()