python学习笔记,我的第一个爬虫

第一次看爬虫的代码,隐隐约约有点像自动化测试的样子
记得刚开始selenium+python的时候,第一课就是让使用python导入selenium中的webdriver,然后启动某个浏览器

from selenium import webdriver
driver = webdriver.firefox()
driver.get("http://www.baidu.com")

然后爬虫的第一步,是打开网页,爬取数据

import urllib.request
response=urllib.request.urlopen("http://www.baidu.com")
html=response.read()
html=html.decode('utf-8')
print(html)

从猫网站上下载一个猫咪的图片

'''from selenium import webdriver
driver = webdriver.firefox()
driver.get("http://www.baidu.com")'''
import urllib.request
response=urllib.request.urlopen("http://placekitten.com/500/600") #将open的内容传递给response对象
cat_img=response.read()
with open('cat_500_600.jpg','wb') as f: #图片属于二进制文件,所以要用wb打开文件
    f.write(cat_img)

print(response.geturl())
print(response.info())
print(response.getcode())

结果:
下载成功图片并打印:

http://placekitten.com/500/600

Date: Fri, 13 Mar 2020 10:47:09 GMT
Content-Type: image/jpeg
Transfer-Encoding: chunked
Connection: close
Set-Cookie: __cfduid=d4f323f3acad3d96ed059f7d239bfe4a01584096429; expires=Sun, 12-Apr-20 10:47:09 GMT; path=/; domain=.placekitten.com; HttpOnly; SameSite=Lax
Cache-Control: public, max-age=86400
Expires: Thu, 31 Dec 2020 20:00:00 GMT
Vary: User-Agent, Accept-Encoding
Access-Control-Allow-Origin: *
CF-Cache-Status: HIT
Age: 14431
Server: cloudflare
CF-RAY: 57352cde1d7b9b9d-SJC

200

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值