標籤(空格分隔): Python 爬蟲 2016年暑假html
import urllib.request url = "http://120.27.101.158/" response = urllib.request.urlopen(url) html = response.read() html = html.decode('utf-8'); print (html)
urllib.request.urlopen()
response
對象也相似與文件對象。等價於
```
req = urllib.request.Request("http://placekitten.com/500/600")
response = urllib.request.urlopen(req)python
response.read()
response
對象的讀操做,相似的文件對象的讀操做.該對象還有如下經常使用方法
```
response.geturl() ##訪問的具體地址。
response.info() ##遠程的服務器的信息
response.getcode() ##http的狀態web
html.decode()
network
中咱們能夠看到一些常見的信息url
地址,http
的狀態(200)json
格式)json
格式用字典傳入一個json
並提交表單,並解析返回來html裏的json
,代碼以下。正則表達式
import urllib.request '''urllib中的parse用來對url解析''' import urllib.parse import json url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null/' content = input("你想翻譯什麼呀?") data = {} data['type']='AUTO' data['i'] = content data['doctype'] = 'json' data['xmlVersion'] = '1.8' data['keyfrom'] = 'fanyi.web' data['ue'] = 'UTF-8' data['typoResult'] = 'true' data = urllib.parse.urlencode(data).encode('utf-8') response = urllib.request.urlopen(url, data) html=response.read().decode('utf-8') target =json.loads(html) print ("翻譯結果是:%s" %(target['translateResult'][0][0]['tgt']))
> print (target) {'translateResult': [[{'src': '測試程序', 'tgt': 'The test program'}]], 'elapsedTime': 0, 'errorCode': 0, 'smartResult': {'entries': ['', '[計] test program'], 'type': 1}, 'type': 'ZH_CN2EN'}
咱們看到翻譯的內容在translateResult[0][0][‘tgt’]中json
data = urllib.parse.urlencode(data).encode('utf-8')
post
,get
進行的字符串,對於中文編碼爲默認格式的字符串。encode
將該字符串轉換爲一個字節序列。(從下面程序能夠看出其實這個utf-8沒什麼卵用,換成gbk還會是同樣的結果)瀏覽器
>data >{'type': 'AUTO', 'ue': 'UTF-8', 'typoResult': 'true', 'i': '程序測試', 'xmlVersion': '1.8', 'keyfrom': 'fanyi.web', 'doctype': 'json'} >data = urllib.parse.urlencode(data); #dict轉換爲str >'type=AUTO&ue=UTF-8&typoResult=true&i=%E7%A8%8B%E5%BA%8F%E6%B5%8B%E8%AF%95&xmlVersion=1.8&keyfrom=fanyi.web&doctype=json' >data = data.encode('utf-8'); #str轉換爲byte序列 >b'type=AUTO&ue=UTF-8&typoResult=true&i=%E7%A8%8B%E5%BA%8F%E6%B5%8B%E8%AF%95&xmlVersion=1.8&keyfrom=fanyi.web&doctype=json'
response = urllib.request.urlopen(url, data)
data
必須爲byte型字符串html=response.read().decode('utf-8')
utf-8
頁面解碼爲unicode
target =json.loads(html)
json
,將其轉換爲字典import re import urllib.parse import urllib.request #----------模擬瀏覽器的行爲,向谷歌翻譯發送數據,而後抓取翻譯結果,這就是大概的思路------- def Gtranslate(text): Gtext=text #text 輸入要翻譯的英文句子 #hl:瀏覽器、操做系統語言,默認是zh-CN #ie:默認是UTF-8 #text:就是要翻譯的字符串 #langpair:語言對,即'en'|'zh-CN'表示從英語到簡體中文 values={'hl':'zh-CN','ie':'UTF-8','text':Gtext,'langpair':"auto"} url='http://translate.google.cn/' #URL用來存儲谷歌翻譯的網址 data = urllib.parse.urlencode(values).encode("utf-8") #將values中的數據經過urllib.urlencode轉義爲URL專用的格式而後賦給data存儲 req = urllib.request.Request(url,data) #而後用URL和data生成一個request browser='Mozilla/4.0 (Windows; U;MSIE 6.0; Windows NT 6.1; SV1; .NET CLR 2.0.50727)' #假裝一個IE6.0瀏覽器訪問,若是不假裝,谷歌將返回一個403錯誤 req.add_header('User-Agent',browser) response = urllib.request.urlopen(req) #向谷歌翻譯發送請求 html=response.read() #讀取返回頁面,而後咱們就從這個HTML頁面中截取翻譯過來的字符串便可 html=html.decode('utf-8') #使用正則表達式匹配<=TRANSLATED_TEXT=)。而翻譯後的文本是'TRANSLATED_TEXT='等號後面的內容 p=re.compile(r"(?<=TRANSLATED_TEXT=).*(?=';INPUT_TOOL_PATH='//www.google.com')") m=p.search(html) chineseText=m.group(0).strip(';') return chineseText if __name__ == "__main__": #Gtext爲待翻譯的字符串 Gtext='我是上帝' print('The input text: %s' % Gtext) chineseText=Gtranslate(Gtext).strip("'") print('Translated End,The output text: %s' % chineseText)
實際的爬蟲十分麻煩,要考慮是否被屏蔽,還有登錄等等問題。待繼續好好學習。服務器
幾個資料網絡
Python網絡爬蟲(Get、Post抓取方式)
py爬取英文文檔學習單詞
python網絡爬蟲入門(二)——用python簡單實現調用谷歌翻譯session