import requests
url= "http://www.antvv.com"
url= "https://www.woyaogexing.com"
r= requests. get( url)
print ( type ( r) )
print ( r. status_code)
print ( r. encoding)
C: \Anaconda3\python. exe D: / AI/ 01 - AI基础/ AI- 3 - Python高级/ 01 爬虫PPT/ day1_爬虫第一天/ day1_爬虫第一天/ demo1_requests请求. py
< class 'requests.models.Response' >
200
ISO- 8859 - 1
Process finished with exit code 0
url= "https://www.zhihu.com"
url= "http://httpbin.org/get"
r= requests. get( url)
headers= {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
}
r= requests. get( url, headers= headers)
print ( r. text)
C: \Anaconda3\python. exe D: / AI/ 01 - AI基础/ AI- 3 - Python高级/ 01 爬虫PPT/ day1_爬虫第一天/ day1_爬虫第一天/ demo1_requests请求. py
{
"args" : { } ,
"headers" : {
"Accept" : "*/*" ,
"Accept-Encoding" : "gzip, deflate" ,
"Host" : "httpbin.org" ,
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" ,
"X-Amzn-Trace-Id" : "Root=1-5ec61689-d50feea5d79d5262f616590e"
} ,
"origin" : "1.199.74.105" ,
"url" : "http://httpbin.org/get"
}
Process finished with exit code 0
url= "http://httpbin.org/get"
headers= {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
}
r= requests. get( url, headers= headers)
json_str= r. text
print ( type ( r. text) )
import json
json_dict= json. loads( json_str)
json_dict= r. json( )
print ( json_dict)
print ( type ( json_dict) )
print ( json_dict[ 'origin' ] )
C: \Anaconda3\python. exe D: / AI/ 01 - AI基础/ AI- 3 - Python高级/ 01 爬虫PPT/ day1_爬虫第一天/ day1_爬虫第一天/ demo1_requests请求. py
< class 'str' >
{ 'args' : { } , 'headers' : { 'Accept' : '*/*' , 'Accept-Encoding' : 'gzip, deflate' , 'Host' : 'httpbin.org' , 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' , 'X-Amzn-Trace-Id' : 'Root=1-5ec616ec-633deb50535c79c0b6312860' } , 'origin' : '1.199.74.105' , 'url' : 'http://httpbin.org/get' }
< class 'dict' >
1.199 .74 .105
Process finished with exit code 0
url= "http://www.521609.com/xuexiaojianjie/yibandaxue/11140.html"
headers= {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
}
r= requests. get( url, headers= headers)
print ( r. text)
print ( r. content)
r= requests. get( url, headers= headers, stream= True )
with open ( "xiaohuar1.jpg" , 'wb' ) as file :
for j in r. iter_content( 10240 ) :
file . write( j)
url= "https://m10.music.126.net/20190824144952/faa8f0ca7ae422e8866afadfa3aa62d5/ymusic/0f53/510b/0709/895a49dd17fa7ae02f850c1b4d4df12e.mp3"
r= requests. get( url, headers= headers, stream= True )
with open ( "孤身.mp3" , 'wb' ) as file :
for j in r. iter_content( 10240 ) :
file . write( j)
url= "http://www.antvv.com/?cate=3"
url= "http://www.antvv.com"
params= {
"cate" : "2"
}
r= requests. get( url, params= params)
print ( r. text)
url= "https://www.zhihu.com"
try :
r= requests. get( url, timeout= 0.1 )
except BaseException:
print ( "超时了," )
else :
print ( r. text)
C: \Anaconda3\python. exe D: / AI/ 01 - AI基础/ AI- 3 - Python高级/ 01 爬虫PPT/ day1_爬虫第一天/ day1_爬虫第一天/ demo1_requests请求. py
超时了,
Process finished with exit code 0
url= "http://httpbin.org/get"
proxies= {
"http" : "183.166.138.176:808"
}
r= requests. get( url, proxies= proxies)
print ( r. text)
url= "http://httpbin.org/post"
data= {
"username" : "admin1234" ,
"password" : "888888"
}
files= {
"html" : open ( "index.html" , 'rb' )
}
r= requests. post( url, data= data, files= files)
print ( r. text)
C: \Anaconda3\python. exe D: / AI/ 01 - AI基础/ AI- 3 - Python高级/ 01 爬虫PPT/ day1_爬虫第一天/ day1_爬虫第一天/ demo1_requests请求. py
{
"args" : { } ,
"data" : "" ,
"files" : {
"html" : "<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n <meta charset=\"UTF-8\">\r\n <title>Title</title>\r\n</head>\r\n<body>\r\n<div>hello world</div>\r\n<a href=\"https://www.baidu.com\">\u767e\u5ea6\u4e00\u4e0b\u4f60\u5c31\u77e5\u9053</a>\r\n<p>\u4eca\u5929\u662f2019\u5e74,\u4eca\u5929\u5f88\u70ed</p>\r\n<p>\u4e0d\u8fc7\u8fd8\u597d,\u6709\u7a7a\u8c03</p>\r\n<img src=\"http://www.antvv.com/zb_users/theme//blacklee/style/images/mlogo.png\">\r\n <li id=\"nvabar-item-index\"><a href=\"http://www.antvv.com/\">\u9996\u9875</a></li>\r\n <li id=\"navbar-page-2\"><a href=\"http://www.antvv.com/?id=2\">\u7559\u8a00\u672c</a></li>\r\n <li id=\"navbar-category-2\"><a href=\"http://www.antvv.com/?cate=2\">PHP</a></li>\r\n <li id=\"navbar-category-3\"><a href=\"http://www.antvv.com/?cate=3\">\u6563\u6587\u968f\u7b14</a></li>\r\n <li id=\"navbar-category-4\"><a href=\"http://www.antvv.com/?cate=4\">Python</a></li>\r\n <li id=\"navbar-category-5\"><a href=\"http://www.antvv.com/?cate=5\">Linux</a></li>\r\n</body>\r\n</html>"
} ,
"form" : {
"password" : "888888" ,
"username" : "admin1234"
} ,
"headers" : {
"Accept" : "*/*" ,
"Accept-Encoding" : "gzip, deflate" ,
"Content-Length" : "1244" ,
"Content-Type" : "multipart/form-data; boundary=e3f9bb77d0c14bd4914d6ea0c4f27c57" ,
"Host" : "httpbin.org" ,
"User-Agent" : "python-requests/2.18.4" ,
"X-Amzn-Trace-Id" : "Root=1-5ec61a0a-54b8dbe8ad0a8d105e84ff70"
} ,
"json" : null,
"origin" : "1.199.74.105" ,
"url" : "http://httpbin.org/post"
}
Process finished with exit code 0