1. 程式人生 > >Python3 urllib.request與requests模組請求網頁程式碼

Python3 urllib.request與requests模組請求網頁程式碼

爬蟲的起點,獲取網頁程式碼

#!/usr/bin/env python3
# -*- encoding:utf-8 -*-

# 請求網頁程式碼

import urllib.request

req = urllib.request.Request('http://www.weather.com.cn/adat/sk/101280101.html')

r = urllib.request.urlopen(req)

result = str(r.read(), encoding='utf-8')

print(result, type(result))


#############################

# requests是第三方模組,與上面urllib.request一樣的效果。而且跟方便簡潔

import
requests res = requests.get("http://www.weather.com.cn/adat/sk/101280101.html") res.encoding = 'utf-8' r = res.text print(type(r), r) #<class 'str'> {"weatherinfo":{"city":"廣州","cityid":"101280101","temp":"24","WD":"東南風","WS":"3級","SD":"80%","WSE":"3","time":"10:25","isRadar":"1","Radar":"JC_RADAR_AZ9200_JB"
,"njd":"暫無實況","qy":"1001"}}