爬蟲基礎---1

Liam_Fang_發表於2019-01-06

 1.安裝requests-----》pip install requests

import requests
r = requests.get("http://www.weather.com.cn/weather/101280601.shtml")
r.encoding="utf-8"
print(r.text)

會出現如下所示的效果

有的網站禁止爬蟲,這樣的話 需要模擬瀏覽器傳送請求,在谷歌瀏覽器輸入chrome://version

 

import requests
//加上header
headers = {"User-Agent":
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36"
}
r = requests.get("https://www.tianqi.com/shenzhen/life.html",headers=headers)
r.encoding="utf-8"
print(r.text)

這樣就能爬取了!!!

相關文章