/Library/Frameworks/Mono.framework/Versions/<Mono Version>/bin/mozroots --import --sync
export PATH="/Library/Frameworks/Mono.framework/Versions/5.20.1/bin:$PATH"
urllib.request.urlopen("網址")
response.read()
response.read().decode("utf-8")
import urllib.request # 1. 建立請求對象(有User-Agent) url = "http://www.baidu.com/" headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"} req = urllib.request.Request(url, headers=headers) # 2. 獲取響應對象 res = urllib.request.urlopen(req) # 3. 讀取響應對象 html = res.read().decode("utf-8") # 4. 具體操做 # print(html) print(res.getcode()) print(res.geturl())
200 https://www.baidu.com/
import urllib.parse meinv = {"wd":"美女"} meinv = urllib.parse.urlencode(meinv) print(meinv)
wd=%E7%BE%8E%E5%A5%B3
import urllib.request import urllib.parse # 拼接URL baseurl = "http://www.baidu.com/s?" key = input("請輸入要搜索的內容:") # 進行urlencode()編碼 key = urllib.parse.urlencode({"wd":key}) url = baseurl + key headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"} # 建立請求對象 req = urllib.request.Request(url, headers=headers) # 獲取響應對象 res = urllib.request.urlopen(req) html = res.read().decode("utf-8") # 寫入文件 with open("搜索.html", "w", encoding='utf-8') as f: f.write(html)
key = urllib.parse.quote("字符串") baseurl = "http://www.baidu.com/s?wd=" key = input("請輸入要搜索的內容:") #進行quote()編碼 key = urllib.parse.quote(key) url = baseurl + key print(url)
GEThtml
POST(在Request方法中添加data參數)python
import urllib.request import urllib.parse import json # 請輸入你要翻譯的內容 key = input("請輸入要翻譯的內容:") # 把提交的form表單數據轉換爲bytes數據類型 data = {'i':key, 'from':'AUTO', 'to':'AUTO', 'smartresult':'dict', 'client':'fanyideskweb', 'salt':'15567713651723', 'sign':'3eee1e0b9cbebd65a65007f497a9b33a', 'ts':'1556771365172', 'bv':'d1dc01b5ffc1e7dfd53e6ee3c347fc81', 'doctype':'json', 'version':'2.1', 'keyfrom':'fanyi.web', 'action':'FY_BY_REALTlME' } # 字符串i=key&from=AUTO&to=AUTO&s.... data = urllib.parse.urlencode(data) data = bytes(data, "utf-8") # 發請求,獲取相應 # url爲POST地址,抓包工具抓到的 url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"} # 此處data爲form表單數據,爲bytes數據類型 req = urllib.request.Request(url, data=data, headers=headers) res = urllib.request.urlopen(req) r_json = res.read().decode("utf-8") r_dict = json.loads(r_json) print(r_dict["translateResult"][0][0]["tgt"])
請輸入要翻譯的內容:天堂 heaven