1.pythonçhttplibï¼urllibåurllib2çåºå«åç¨
2.关于python中urllib2.urlopen函数结果复制的源码问题。
pythonçhttplibï¼urllibåurllib2çåºå«åç¨
å®è¿°
é¦å æ¥çä¸ä¸ä»ä»¬çåºå«
urllibåurllib2
urllib åurllib2é½æ¯æ¥åURL请æ±çç¸å ³æ¨¡åï¼ä½æ¯urllib2å¯ä»¥æ¥åä¸ä¸ªRequestç±»çå®ä¾æ¥è®¾ç½®URL请æ±çheadersï¼urllibä» å¯ä»¥æ¥åURLã
è¿æå³çï¼ä½ ä¸å¯ä»¥ä¼ªè£ ä½ çUser Agentå符串çã
urllibæä¾urlencodeæ¹æ³ç¨æ¥GETæ¥è¯¢å符串ç产çï¼èurllib2没æãè¿æ¯ä¸ºä½urllib常åurllib2ä¸èµ·ä½¿ç¨çåå ã
ç®åç大é¨åpatible; MSIE 5.5; Windows NT)'# å°user_agentåå ¥å¤´ä¿¡æ¯
values = { 'name' : 'who',源码成本价源码'password':''}
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
valuesæ¯postæ°æ®
GETæ¹æ³
ä¾å¦ç¾åº¦ï¼
ï¼è¿æ ·æ们éè¦å°{ âwdâ:âxxxâ}è¿ä¸ªåå ¸è¿è¡urlencode
#coding:utf-8
import urllib
import urllib2
url = ''
values = { 'wd':'D_in'}
data = urllib.urlencode(values)
print data
url2 = url+'?'+data
response = urllib2.urlopen(url2)
the_page = response.read()
print the_page
POSTæ¹æ³
import urllib
import urllib2
url = ''
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' //å°user_agentåå ¥å¤´ä¿¡æ¯
values = { 'name' : 'who','password':''} //postæ°æ®
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values) //对postæ°æ®è¿è¡urlç¼ç
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
urllib2带cookieç使ç¨
#coding:utf-8
import urllib2,urllib
import cookielib
url = r''
#å建ä¸ä¸ªcjçcookieç容å¨
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#å°è¦POSTåºå»çæ°æ®è¿è¡ç¼ç
data = urllib.urlencode({ "email":email,"password":pass})
r = opener.open(url,data)
print cj
/')
baidu_data = baidu_doc.read()
baidu_soup = BeautifulSoup(baidu_data)
baidu_doc1 = copy.copy(baidu_doc)
print baidu_doc1
===>输出这么个指针吧 fp = <socket._fileobject object at 0xC3F0>
baidu_doc 本来就是个指针
2024-11-20 21:391323人浏览
2024-11-20 21:312794人浏览
2024-11-20 21:16722人浏览
2024-11-20 20:501936人浏览
2024-11-20 20:492841人浏览
2024-11-20 20:402965人浏览
中国消费者报上海讯(记者刘浩)第三届中国国际进口博览会开幕在即,各项筹备工作正式进入冲刺阶段。10月26日,为迎接第三届进博会,落实进博会服务保障要求,上海市浦东新区市场监管局联合浦东商务委,举办了&
美國大選出現震撼彈,總統拜登Joe Biden)宣布退選,並表態支持副總統賀錦麗,消息一公布,美元指數立刻大跳水,亞洲貨幣卻沒有因此走升,因為不確定因素大增,造成股匯市受到影響,新台幣週一22日)匯率