網絡爬蟲--4.requests的簡單實用

文章目錄

  • 一.簡介
  • 二.基本GET請求
    • 1.最基本的GET請求--直接用get方法
    • 2.添加 headers 和查詢參數parmas
    • 3.通過requests獲取網絡上圖片的大小
  • 三.基本POST請求
    • 1.傳入data數據
  • 四.代理(proxies參數)
  • 五.私密代理
  • 六.web客戶端驗證
  • 七.Cookies 和 Sission
    • 1.Cookies
    • 2.Session
  • 八.處理HTTPS請求 SSL證書驗證

一.簡介

雖然Python的標準庫中 urllib 模塊已經包含了平常我們使用的大多數功能,但是它的 API 使用起來讓人感覺不太好,而 Requests 自稱 “HTTP for Humans”,說明使用更簡潔方便。

requests 的底層實現其實就是 urllib,它繼承了urllib的所有特性。Requests支持HTTP連接保持和連接池,支持使用cookie保持會話,支持文件上傳,支持自動確定響應內容的編碼,支持國際化的 URL 和 POST 數據自動編碼。

利用 pip 安裝 或者利用 easy_install 都可以完成安裝:

$ pip install requests$ easy_install requests

二.基本GET請求

1.最基本的GET請求–直接用get方法

response.text和response.content的區別:
response.content:這個是直接從網絡上面抓取的數據。沒有經過任何解碼。所以是一個bytes類型。其實在硬盤上和在網絡上傳輸的字符串都是bytes類型。
response.text:這個是str的數據類型,是requests庫將response.content進行解碼的字符串。解碼需要指定一個編碼方式,requests會根據自己的猜測來判斷編碼的方式。所以有時候可能會猜測錯誤,就會導致解碼產生亂碼。這時候就應該使用response.content.decode(‘utf-8’)進行手動解碼。

import requestsresponse = requests.get('http://www.baidu.com/')print('----------')
print(response.request.headers)
print('----------')
print(response.text)
print('----------')
print(response.content)

輸出結果:

----------
{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
----------
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>????o|??€???????? ?°±??¥é?“</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=????o|??€??? class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>?–°é—?</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>??°???</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>è§?频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>è′′??§</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>??????</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">??????</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">??′?¤??o§?“?</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>?
3?o?????o|</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>????”¨????o|?‰???
èˉ?</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>???è§????é|?</a>&nbsp;?o?ICPèˉ?030173??·&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>----------
b'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>\xe6\x96\xb0\xe9\x97\xbb</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>\xe5\x9c\xb0\xe5\x9b\xbe</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>\xe8\xa7\x86\xe9\xa2\x91</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>\xe8\xb4\xb4\xe5\x90\xa7</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>\xe7\x99\xbb\xe5\xbd\x95</a> </noscript> <script>document.write(\'<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\'+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ \'" name="tj_login" class="lb">\xe7\x99\xbb\xe5\xbd\x95</a>\');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">\xe6\x9b\xb4\xe5\xa4\x9a\xe4\xba\xa7\xe5\x93\x81</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>\xe5\x85\xb3\xe4\xba\x8e\xe7\x99\xbe\xe5\xba\xa6</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>\xe4\xbd\xbf\xe7\x94\xa8\xe7\x99\xbe\xe5\xba\xa6\xe5\x89\x8d\xe5\xbf\x85\xe8\xaf\xbb</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>\xe6\x84\x8f\xe8\xa7\x81\xe5\x8f\x8d\xe9\xa6\x88</a>&nbsp;\xe4\xba\xacICP\xe8\xaf\x81030173\xe5\x8f\xb7&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n'

2.添加 headers 和查詢參數parmas

如果想添加 headers,可以傳入headers參數來增加請求頭中的headers信息。如果要將參數放在url中傳遞,可以利用 params 參數。

import requestskw = {'wd':'長城'}headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}# params 接收一個字典或者字符串的查詢參數,字典類型自動轉換為url編碼,不需要urlencode()
response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers)print('-----1-----')
print(response.request.headers)# 查看響應內容,response.text 返回的是Unicode格式的數據
print('-----2-----')
print (response.text)# # 查看響應內容,response.content返回的字節流數據
print('-----3-----')
print (response.content)print('-----4-----')
print (response.content.decode())# 查看完整url地址
print('----------------------5----------------------------')
print (response.url)# 查看響應頭部字符編碼
print('-----6-----')
print (response.encoding)# 查看響應碼
print('-----7-----')
print (response.status_code)

輸出結果:

-----1-----
{'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
-----2-----
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><title>????o|??‰?
¨éa?èˉ?</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="format-detection" content="telephone=no, email=no"><link rel="shortcut icon" href="https://www.baidu.com/favicon.ico" type="image/x-icon"><link rel="icon" sizes="any" mask href="https://www.baidu.com/img/baidu.svg"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><link rel="stylesheet" href="https://wappass.bdimg.com/static/touch/css/api/mkdjump_8befa48.css" />
</head>
<body><div class="timeout hide"><div class="timeout-img"></div><div class="timeout-title">??‘???????????????èˉ·?¨????é??èˉ?</div><button type="button" class="timeout-button">è?”???é|–é?μ</button></div><div class="timeout-feedback hide"><div class="timeout-feedback-icon"></div><p class="timeout-feedback-title">é—?é¢????é|?</p></div><script src="https://wappass.baidu.com/static/machine/js/api/mkd.js"></script>
<script src="https://wappass.bdimg.com/static/touch/js/mkdjump_6003cf3.js"></script>
</body>
</html>
-----3-----
b'<!DOCTYPE html>\n<html lang="zh-CN">\n<head>\n    <meta charset="utf-8">\n    <title>\xe7\x99\xbe\xe5\xba\xa6\xe5\xae\x89\xe5\x85\xa8\xe9\xaa\x8c\xe8\xaf\x81</title>\n    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n    <meta name="apple-mobile-web-app-capable" content="yes">\n    <meta name="apple-mobile-web-app-status-bar-style" content="black">\n    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">\n    <meta name="format-detection" content="telephone=no, email=no">\n    <link rel="shortcut icon" href="https://www.baidu.com/favicon.ico" type="image/x-icon">\n    <link rel="icon" sizes="any" mask href="https://www.baidu.com/img/baidu.svg">\n    <meta http-equiv="X-UA-Compatible" content="IE=Edge">\n    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">\n    <link rel="stylesheet" href="https://wappass.bdimg.com/static/touch/css/api/mkdjump_8befa48.css" />\n</head>\n<body>\n    <div class="timeout hide">\n        <div class="timeout-img"></div>\n        <div class="timeout-title">\xe7\xbd\x91\xe7\xbb\x9c\xe4\xb8\x8d\xe7\xbb\x99\xe5\x8a\x9b\xef\xbc\x8c\xe8\xaf\xb7\xe7\xa8\x8d\xe5\x90\x8e\xe9\x87\x8d\xe8\xaf\x95</div>\n        <button type="button" class="timeout-button">\xe8\xbf\x94\xe5\x9b\x9e\xe9\xa6\x96\xe9\xa1\xb5</button>\n    </div>\n    <div class="timeout-feedback hide">\n        <div class="timeout-feedback-icon"></div>\n        <p class="timeout-feedback-title">\xe9\x97\xae\xe9\xa2\x98\xe5\x8f\x8d\xe9\xa6\x88</p>\n    </div>\n\n<script src="https://wappass.baidu.com/static/machine/js/api/mkd.js"></script>\n<script src="https://wappass.bdimg.com/static/touch/js/mkdjump_6003cf3.js"></script>\n</body>\n</html>'
-----4-----
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><title>百度安全驗證</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="format-detection" content="telephone=no, email=no"><link rel="shortcut icon" href="https://www.baidu.com/favicon.ico" type="image/x-icon"><link rel="icon" sizes="any" mask href="https://www.baidu.com/img/baidu.svg"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><link rel="stylesheet" href="https://wappass.bdimg.com/static/touch/css/api/mkdjump_8befa48.css" />
</head>
<body><div class="timeout hide"><div class="timeout-img"></div><div class="timeout-title">網絡不給力,請稍后重試</div><button type="button" class="timeout-button">返回首頁</button></div><div class="timeout-feedback hide"><div class="timeout-feedback-icon"></div><p class="timeout-feedback-title">問題反饋</p></div><script src="https://wappass.baidu.com/static/machine/js/api/mkd.js"></script>
<script src="https://wappass.bdimg.com/static/touch/js/mkdjump_6003cf3.js"></script>
</body>
</html>
----------------------5----------------------------
https://wappass.baidu.com/static/captcha/tuxing.html?&ak=c27bbc89afca0463650ac9bde68ebe06&backurl=https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%25E9%2595%25BF%25E5%259F%258E&logid=9931754313911722977&signature=e44e6266c70740aa0955def31c517835&timestamp=1587817039
-----6-----
ISO-8859-1
-----7-----
200

3.通過requests獲取網絡上圖片的大小

from io import BytesIO,StringIO
import requests
from PIL import Image
img_url = "http://imglf1.ph.126.net/pWRxzh6FRrG2qVL3JBvrDg==/6630172763234505196.png"
response = requests.get(img_url)
f = BytesIO(response.content)
img = Image.open(f)
print(img.size)

輸出結果:

(500, 262)

理解一下 BytesIO 和StringIO

很多時候,數據讀寫不一定是文件,也可以在內存中讀寫。

StringIO顧名思義就是在內存中讀寫str。
BytesIO 就是在內存中讀寫bytes類型的二進制數據

例子中如果使用StringIO 即f = StringIO(response.text)會產生"cannot identify image file"的錯誤
當然上述例子也可以把圖片存到本地之后再使用Image打開來獲取圖片大小

三.基本POST請求

1.傳入data數據

對于 POST 請求來說,我們一般需要為它增加一些參數。那么最基本的傳參方法可以利用 data 這個參數。

import requestsformdata = {"type":"AUTO","i":"i love python","doctype":"json","xmlVersion":"1.8","keyfrom":"fanyi.web","ue":"UTF-8","action":"FY_BY_ENTER","typoResult":"true"
}url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}response = requests.post(url, data = formdata, headers = headers)print ('-------------1-----------')
print (response.text)# 如果是json文件可以直接顯示
print ('-------------2----------')
print (response.json())

運行結果:

{"type":"EN2ZH_CN","errorCode":0,"elapsedTime":3,"translateResult":[[{"src":"i love python","tgt":"我喜歡python"}]],"smartResult":{"type":1,"entries":["","肆文","","","高德納","",""]}}{'type': 'EN2ZH_CN', 'errorCode': 0, 'elapsedTime': 3, 'translateResult': [[{'src': 'i love python', 'tgt': '我喜歡python'}]], 'smartResult': {'type': 1, 'entries': ['', '肆文', '', '', '高德納', '', '']}}

四.代理(proxies參數)

如果需要使用代理,你可以通過為任意請求方法提供 proxies 參數來配置單個請求:

import requests# 根據協議類型,選擇不同的代理
proxies = {"http": "http://12.34.56.79:9527","https": "http://12.34.56.79:9527",
}response = requests.get("http://www.baidu.com", proxies = proxies)
print response.text

也可以通過本地環境變量 HTTP_PROXY 和 HTTPS_PROXY 來配置代理:

export HTTP_PROXY="http://12.34.56.79:9527"
export HTTPS_PROXY="https://12.34.56.79:9527"

五.私密代理

import requests# 如果代理需要使用HTTP Basic Auth,可以使用下面這種格式:
proxy = { "http": "mr_mao_hacker:sffqry9r@61.158.163.130:16816" }response = requests.get("http://www.baidu.com", proxies = proxy)print (response.text)

六.web客戶端驗證

如果是Web客戶端驗證,需要添加 auth = (賬戶名, 密碼)

import requestsauth=('test', '123456')response = requests.get('http://192.168.199.107', auth = auth)print (response.text)

七.Cookies 和 Sission

1.Cookies

如果一個響應中包含了cookie,那么我們可以利用 cookies參數拿到:

import requestsresponse = requests.get("http://www.baidu.com/")# 7\. 返回CookieJar對象:
cookiejar = response.cookies# 8\. 將CookieJar轉為字典:
cookiedict = requests.utils.dict_from_cookiejar(cookiejar)print (cookiejar)print (cookiedict)

運行結果:

<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>{'BDORZ': '27315'}

2.Session

在 requests 里,session對象是一個非常常用的對象,這個對象代表一次用戶會話:從客戶端瀏覽器連接服務器開始,到客戶端瀏覽器與服務器斷開。

會話能讓我們在跨請求時候保持某些參數,比如在同一個 Session 實例發出的所有請求之間保持 cookie 。

實現人人網登錄:

import requests# 1\. 創建session對象,可以保存Cookie值
ssion = requests.session()# 2\. 處理 headers
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}# 3\. 需要登錄的用戶名和密碼
data = {"email":"mr_mao_hacker@163.com", "password":"alarmchime"}  # 4\. 發送附帶用戶名和密碼的請求,并獲取登錄后的Cookie值,保存在ssion里
ssion.post("http://www.renren.com/PLogin.do", data = data)# 5\. ssion包含用戶登錄后的Cookie值,可以直接訪問那些登錄后才可以訪問的頁面
response = ssion.get("http://www.renren.com/410043129/profile")# 6\. 打印響應內容
print (response.text)

八.處理HTTPS請求 SSL證書驗證

Requests也可以為HTTPS請求驗證SSL證書:

要想檢查某個主機的SSL證書,你可以使用 verify 參數(也可以不寫)

import requests
response = requests.get("https://www.baidu.com/", verify=True)# 也可以省略不寫
# response = requests.get("https://www.baidu.com/")
print (r.text)

運行結果:

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type
content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible
content=IE=Edge>百度一下,你就知道 ....

如果SSL證書驗證不通過,或者不信任服務器的安全證書,則會報出SSLError

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/452080.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/452080.shtml
英文地址,請注明出處:http://en.pswp.cn/news/452080.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

編程各類資源大放送

小編的公眾號[編程軟文]一直在運營著&#xff0c;但是我的公眾號只是發一些技術類文章&#xff0c;沒有特地為自己的公眾號如何添粉去操作&#xff0c;目前公眾號關注度比較低。看著其他人的公眾號粉絲越來越多&#xff0c;小編也需要為自己做些事。為了提高自己公眾號的關注度…

使用Spring框架能帶來那些好處?

1、Dependency Injection(DI)方法使得構造器和JavaBean properties文件中的依賴關系一目了然。2、與EJB容器相比較&#xff0c;Ioc容器更加趨向于輕量級。這樣一來Ioc容器在郵箱的內存和CPU資源的情況下進行應用程序的開發和發布就變得十分有利。3、Spring并沒有閉門造車&#…

誰在告誰?移動專利混戰圖

移動領域激戰正酣&#xff0c;同樣是沒有永遠的朋友&#xff0c;只有永遠的利益。 蘋果剛剛起訴三星的Galaxy手機和平板電腦山寨了蘋果的產品&#xff0c;而此前兩家并沒有過節。再比如微軟和亞馬遜以及HTC之間的授權協議爭端。移動領域的爭端如此之多&#xff0c;以至于看客無…

java中list()和listfile()

File fnew File("c:\\");String[] f1f.list();File[] f2f.listFiles();① list() 返回一個字符串數組&#xff0c;這些字符串指定此抽象路徑名表示的目錄中的文件和目錄。 以C盤為例&#xff0c;返回的是c盤下文件夾名字的字符串數組,如[TEMP, Windows]②listFile…

光棍節程序員闖關秀過關全攻略

maven/java/web/bootstrapQQ群&#xff1a;566862629。希望更多人一起幫助我學習。 光棍節程序員闖關秀過關全攻略。程序員的寂寞誰能懂?"SF光棍節程序員闖關秀"智力挑戰小游戲火熱上線&#xff0c;看看你能闖到第幾關&#xff1f; 游戲地址: http://segmentfault…

jekins搭建

2019獨角獸企業重金招聘Python工程師標準>>> 轉自 https://www.cnblogs.com/hdwang/p/6081994.html &#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xf…

網絡爬蟲--5.urllib庫的基本使用(1)

文章目錄一. 前言二. urlopen三. Request四. User-Agent五. 添加更多的Header信息1. 添加一個特定的header2. 隨機添加/修改User-Agent一. 前言 所謂網頁抓取&#xff0c;就是把URL地址中指定的網絡資源從網絡流中讀取出來&#xff0c;保存到本地。 在Python中有很多庫可以用來…

高性能計算

信息時代的硬件芯片和存儲器價格以摩爾定律的形式下降&#xff0c;可是現在處理的數據量也越來越大。我們先以cocoa編程為例&#xff0c;然后再結合網格計算、云計算&#xff0c;綜合對最新的高性能計算技術作介紹。 使用 runloop 在cocoa編程如果用NSThread開線程 [NSThread …

混合型面向對象語言和純面向對象語言

20世紀80年代以來&#xff0c;面向對象語言像雨后春筍一樣大量涌現&#xff0c;形成了兩大類面向對象語言。 一類是純面向對象語言&#xff0c;如Smalltalk和Eiffel等語言 另一類是混合型面向對象語言&#xff0c;也就是在過程語言的基礎上增加面向對象機制&#xff0c;如C等…

塊級元素的margin-left和margin-right的用法注意

此時是有效果顯示的因為html文檔流默認是從上往下&#xff0c;從左往右進行顯示的&#xff0c;所以此時是有效果的。那如果此時把#son的塊元素的margin-right:20px; 是沒有效果的此時是沒有效果的&#xff0c;如圖所示&#xff1a;如果此時想要margin-right有效果的話&#xf…

Apache Tiles的基本使用

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1、概述 對于一個新的技術&#xff0c;了解其基本的概念和和原理是學好該技術的基礎。 2、Tiles的概念 Tiles 是復合視圖模式&#xff0…

js--------1.時間

1 //獲取當前時間 yyyy-MM-dd2 function getNowFormatDate() {3 var date new Date();4 var seperator1 "-";5 var seperator2 ":";6 var month date.getMonth() 1;7 var strDate date.getDate();8 if (month > 1 &&…

網絡爬蟲--6.urllib庫的基本使用(2)

文章目錄一. urllib.parse.urlencode()和urllib.parse.unquote()二. Get方式三. 批量爬取百度貼吧數據四.POST方式五.關于CA六.處理HTTPS請求 SSL證書驗證一. urllib.parse.urlencode()和urllib.parse.unquote() 編碼工作使用urllib.parse的urlencode()函數&#xff0c;幫我們…

面向對象語言的技術特點

1.支持類與對象概念的機制 所有面向對象語言都允許用戶動態創建對象&#xff0c;并且可以用指針引用動態創建的對象。允許動態創建對象&#xff0c;就意味著系統必須處理內存管理問題&#xff0c;如果不及時釋放不再需要的對象所占用的內存&#xff0c;動態存儲分配就有可能耗…

INI 文件的操作

在程序中經常要用到設置或者其他少量數據的存盤&#xff0c;以便程序在下一次執行的時候可以使用&#xff0c;比如說保存本次程序執行時窗口的位置、大小、一些用戶設置的數據等等&#xff0c;在 Dos 下編程的時候&#xff0c;我們一般自己產生一個文件&#xff0c;由自己把這些…

摩拜大數據殺熟?官方:老用戶押金的確退款延遲

近日&#xff0c;有媒體曝出摩拜單車一些老用戶出現押金難退現象。有的消費者點擊退款后&#xff0c;系統不斷奔潰&#xff1b;有的申請退款后&#xff0c;賬戶又莫名出現押金&#xff0c;就像未申請一樣&#xff1b;也有人終于提交了退款&#xff0c;等候數日卻遲遲不見到賬。…

Junit Test使用樣例

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 配置&#xff1a; 調用類&#xff1a; import java.util.List;import javax.annotation.Resource;import org.apache.shiro.crypto.Rand…

Django congtent types應用

contenttypes 是Django內置的一個應用&#xff0c;可以追蹤項目中所有app和model的對應關系&#xff0c;并記錄在ContentType表中。 每當我們創建了新的model并執行數據庫遷移后&#xff0c;ContentType表中就會自動新增一條記錄。比如我在應用app01的models.py中創建表class E…

網絡爬蟲--7.Handler處理器 和 自定義Opener

文章目錄一. 引言二. 簡單的自定義opener()三. ProxyHandler處理器&#xff08;代理設置&#xff09;四. Cookie1.Cookie原理2.Cookie應用五. cookiejar庫 和 HTTPCookieProcessor處理器1.案例一:獲取Cookie&#xff0c;并保存到CookieJar()對象中2.案例二&#xff1a;利用cook…

如何選擇面向對象語言

開發人員在選擇面向對象語言時&#xff0c;還應該著重考慮以下一些實際因素。 1. 將來能否占主導地位 為了使自己的產品在若干年后仍然具有很強的生命力&#xff0c;人們可能希望采用將來占主導地位的語言編程。 根據目前占有的市場份額&#xff0c;以及專業書刊和學術會議上所…