wd=「要搜索的內容」
q=「要搜索的內容」
params
參數。只須構造鍵值對,提交給params
便可。>>> import requests >>> kv = {'wd':'python'} >>> r = requests.get("http://www.baidu.com/s",params=kv) >>> r.status_code 200
response
對象中的request
對象來查看咱們提交給服務器的URL連接。>>> r.request.url 'http://www.baidu.com/s?wd=python'
import requests kv = {'wd':'python'} url = "http://www.baidu.com/s" try: r = requests.get(url,params = kv) print(r.request.url) r.raise_for_status() print(r.text[:2000]) except: print("爬取失敗")
url+xxxx.jpg
>>> import requests >>> path = "/Users/hyliu/Desktop/picture.jpg" >>> url = "https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=3acf58502f7f9e2f6438155a7e598241/7aec54e736d12f2e227c44d647c2d5628535680f.jpg" >>> r = requests.get(url) >>> r.status_code 200
r
中已經包含了咱們想要的圖片,接下來要作的事情就是把圖片變成文件存放到本機。>>> path = "/Users/hyliu/Desktop/picture.jpg" >>> with open(path,'wb') as f: f.write(r.content) 53658 >>>
picture.jpg
,並定義爲一個文件標識符f
。而後咱們將返回的內容寫入到這個文件中。r.content
表示返回內容的二進制格式,因此咱們將r.content
寫入到圖片文件中。>>> f.close() >>>
import requests import os url = "https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=3acf58502f7f9e2f6438155a7e598241/7aec54e736d12f2e227c44d647c2d5628535680f.jpg" root = "/Users/hyliu/Desktop/" path = root + url.split('/')[-1] #獲取URL最後一個「/」後的內容,實際上就是獲取圖片本來的名字 try: if not os.path.exists(root): #目錄不存在則建立 os.mkdir(root) if not os.path.exists(path): #判斷是否存在重名文件 r = requests.get(url) #print(r.status_code) r.raise_for_status() with open (path,'wb') as f: f.write(r.content) f.close() print("文件保存成功!") else: print("文件已存在") except: print("爬取失敗")
>>> import requests >>> url = "http://m.ip138.com/ip.asp" >>> kv = {'ip':'218.106.145.15'} >>> r = requests.get(url,params = kv) >>> r.status_code 200 >>> r.text[-500:] 'submit" value="查詢" class="form-btn" />\r\n\t\t\t\t\t</form>\r\n\t\t\t\t</div>\r\n\t\t\t\t<div class="query-hd">ip138.com IP查詢(搜索IP地址的地理位置)</div>\r\n\t\t\t\t<h1 class="query">您查詢的IP:218.106.145.15</h1><p class="result">本站主數據:福建省福州市 聯通</p><p class="result">參考數據一:福建省福州市 聯通</p>\r\n\r\n\t\t\t</div>\r\n\t\t</div>\r\n\r\n\t\t<div class="footer">\r\n\t\t\t<a href="http://www.miitbeian.gov.cn/" rel="nofollow" target="_blank">滬ICP備10013467號-1</a>\r\n\t\t</div>\r\n\t</div>\r\n\r\n\t<script type="text/javascript" src="/script/common.js"></script></body>\r\n</html>\r\n' >>>
import requests kv = {'ip':'218.106.145.15'} url = "http://m.ip138.com/ip.asp" try: r = requests.get(url,params = kv) r.raise_for_status() print(r.text[-500:]) except: print("爬取失敗")
URL is APIjavascript