第一種 User-Agent方法:
創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站設(shè)計、成都做網(wǎng)站與策劃設(shè)計,武鄉(xiāng)網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:武鄉(xiāng)等地區(qū)。武鄉(xiāng)做網(wǎng)站價格咨詢:18982081108
import urllib2
if __name__ == '__main__' :
url = '想訪問的網(wǎng)址'
head ={}
head['User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19'
req = urllib2.Request(url,headers=head)
response = urllib2.urlopen(req)
html = response.read().decode('utf-8') #根據(jù)網(wǎng)站的編碼格式,選擇?!甎TF-8’,‘GBK’等
print html
第二種 User-Agent 方法:
import urllib2
if __name__ == '__main__':
url = '想訪問的網(wǎng)址'
req = urllib2.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19')
response = urllib2.urlopen(req)
html = response.read().decode('utf-8')
print html