Python 爬虫笔记(1)

import urllib.request
访问网址,打开网页,方法:
urllib.request.urlopen(url,data=None, [timeout, ]*, cafile=None, capath=None,cadefault=False, context=None)
其中url,可以是一个字符串string(也就是网址),也可以是一个Request对象
方法一:
response = urllib.request.urlopen("http://www.baidu.com")

html = response.read()
html = html.dcode("utf-8")/#按照网页代码,整齐显示
print(html)
方法二:
req = urllib.request.Request("http://www.baidu.com")
response = urllib.request.urlopen(req)
html = response.read()
print(html)
方法一,会把网址首先转化为一个对象,也就是方法二,然后,对对象进行访问。
下载一只猫,网址:http://placekitten.com
req = urllib.request.Request("http://placekitten.com/g/500_600")
response = urllib.request.urlopen(req)
cat_img = response.read()

with open('cat_img_500_600.jpg', 'wb') as f:
     f.write(cat_img)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值