大佬教程收集整理的这篇文章主要介绍了【Python自学笔记】10个爬虫入门实例,附源码,注释对新手友好,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
代码中给出了注释c;并且可以直接运行哦
如何安装requests库c;没有的c;建议先装一下Python环境c;windows用户c;Linux用户几乎一样:
1.打开cmd输入以下命令即可c;如果python的环境在C盘的目录c;会提示权限不够c;只需以管理员方式运行cmd窗口
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
2.Linux用户类似(ubantu为例): 权限不够的话在命令前加入sudo即可
sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
# 第一个爬虫示例,爬取百度页面
import requests #导入爬虫的库c;不然调用不了爬虫的函数
response = requests.get("http://www.baidu wangt.cc ") #生成一个response对象
response.encoding = response.apparent_encoding #设置编码格式
print("状态码:"+ str( response.status_code ) ) #打印状态码
print(response.text)#输出爬取的信息
# 第二个get方法实例
import requests #先导入爬虫的库c;不然调用不了爬虫的函数
response = requests.get("http://httpbin.org/get") #get方法
print( response.status_code ) #状态码
print( response.text )
# 第三个 post方法实例
import requests #先导入爬虫的库c;不然调用不了爬虫的函数
response = requests.post("http://httpbin.org/post") #post方法访问
print( response.status_code ) #状态码
print( response.text )
# 第四个 put方法实例
import requests #先导入爬虫的库c;不然调用不了爬虫的函数
response = requests.put("http://httpbin.org/put") # put方法访问
print( response.status_code ) #状态码
print( response.text )
如果需要传多个参数只需要用&符号连接即可如下
# 第五个 get传参方法实例
import requests #先导入爬虫的库c;不然调用不了爬虫的函数
response = requests.get("http://httpbin.org/get?name=hezhi&age=20") # get传参
print( response.status_code ) #状态码
print( response.text )
params用字典可以传多个
# 第六个 get传参方法实例
import requests #先导入爬虫的库c;不然调用不了爬虫的函数
data = {
"name":"hezhi",
"age":20
}
response = requests.get( "http://httpbin.org/get" , params=data ) # get传参
print( response.status_code ) #状态码
print( response.text )
和上一个有没有很像
# 第七个 post传参方法实例
import requests #先导入爬虫的库c;不然调用不了爬虫的函数
data = {
"name":"hezhi",
"age":20
}
response = requests.post( "http://httpbin.org/post" , params=data ) # post传参
print( response.status_code ) #状态码
print( response.text )
# 第好几个方法实例
import requests #先导入爬虫的库c;不然调用不了爬虫的函数
response = requests.get( "http://www.zhihu wangt.cc ") #第一次访问知乎c;不设置头部信息
print( "第一次,不设头部信息,状态码:"+response.status_code )# 没写headersc;不能正常爬取c;状态码不是 200
#下面是可以正常爬取的区别c;更改了User-Agent字段
headers = {
"user-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"
}#设置头部信息,伪装浏览器
response = requests.get( "http://www.zhihu wangt.cc " , headers=headers ) #get方法访问,传入headers参数c;
print( response.status_code ) # 200!访问成功的状态码
print( response.text )
因为目录关系c;在D盘建立了一个叫做爬虫的文件夹c;然后保存信息c;注意文件保存时的encoding设置
# 爬取一个html并保存
import requests
url = "http://www.baidu wangt.cc "
response = requests.get( url )
response.encoding = "utf-8" #设置接收编码格式
print("nr的类型" + str( type(response) ) )
print("n状态码是:" + str( response.status_code ) )
print("n头部信息:" + str( response.headers ) )
print( "n响应内容:" )
print( response.text )
#保存文件
file = open("D:\爬虫\baidu.html","w",encoding="utf") #打开一个文件c;w是文件不存在则新建一个文件c;这里不用wb是因为不用保存成二进制
file.write( response.text )
file.close()
#保存百度图片到本地
import requests #先导入爬虫的库c;不然调用不了爬虫的函数
response = requests.get("https://www.baidu wangt.cc /img/baidu_jgylogo3.gif") #get方法的到图片响应
file = open("D:\爬虫\baidu_logo.gif","wb") #打开一个文件,wb表示以二进制格式打开一个文件只用于写入
file.write(response.content) #写入文件
file.close()#关闭操作c;运行完毕后去你的目录看一眼有没有保存成功
自学的道路很枯燥c;但坚持下来眼前会一片光明与希望c;咸鱼与翻身就在一念之间c;但能坚持下来的才最可怕c;加油。
感谢观看c;更多Python精彩内容可以看小编主页。
以上是大佬教程为你收集整理的【Python自学笔记】10个爬虫入门实例,附源码,注释对新手友好全部内容,希望文章能够帮你解决【Python自学笔记】10个爬虫入门实例,附源码,注释对新手友好所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。