#requests.get方法
import requests
req=requests.get('http://www.baidu.com')
req.encoding='utf-8'
print(req.status_code)
type(req)
req.headers#頁(yè)面頭部信息
req.text
import requests
def gethtml(url):
? ? try:
? ? ? ? req=requests.get(url)
? ? ? ? req.raise_for_status()
? ? ? ? req.encoding=req.apparent_encoding
? ? ? ? print(req.text[:1000])
? ? except:
? ? ? ? print('爬蟲(chóng)異常')
url='https://item.jd.com/5089237.html'
gethtml(url)
#偽裝瀏覽器訪(fǎng)問(wèn)
def gethtml(url):
? ? try:
? ? ? ? head={'User-Agent':'Mozilla/5.0 '}
? ? ? ? req=requests.get(url,headers=head)
? ? ? ? req.raise_for_status()
? ? ? ? req.encoding=req.apparent_encoding
? ? ? ? print(req.text[:100])
? ? except:
? ? ? ? print('爬蟲(chóng)異常')
url='https://item.jd.com/5089237.html'
gethtml(url)
req.request.headers#查看爬蟲(chóng)請(qǐng)求的頭部信息
#加入關(guān)鍵詞搜索
def html(url):
? ? try:
? ? ? ? head={'User-Agent':'Mozilla/5.0 '}
? ? ? ? kd={'wd':'python'}
? ? ? ? r=requests.get(url,params=kd,headers=head)
? ? ? ? print(r.request.url)
? ? ? ? r.raise_for_status()
? ? ? ? print(len(r.text))
? ? except:
? ? ? ? print('爬蟲(chóng)異常')
url='http://www.baidu.com/s'
html(url)
#json數(shù)據(jù)格式
import json
data='{"購(gòu)買(mǎi)方式":"裸機(jī)","版本":"6GB+64GB","skuId":7437788,"顏色":"黑"}'
jdata=json.loads(data)
jdata.keys()
jdata['版本']