[学习笔记]python爬虫初体验

同学吹水,提到了爬虫,于是金工实习回来晚上看了看爬虫
(话说为啥所有爬虫教程前面都是一大串python基础教程啊)

爬虫就是*****
就是获取网页信息的工具,开始学习的话先使用python自带的urllib库进行coding,以后再了解requests等东西
感觉爬虫入门不难,只要发挥我api小能手的特长就行了(笑)
下面贴一下最最基础的一份代码,可以爬取网页的html源代码:

import urllib.request
#1、定义一个网址url
url='http://www.baidu.com'
#2、模拟浏览器向服务器发送请求
response = urllib.request.urlopen(url)

print(type(response))

#一个字节一个字节读入 
# content = response.read()
#读取括号内个字节的字符
# content = response.read(5)
#一行读取
# content = response.readline()
#一行一行读取,直到最后
# content = response.readlines()

#获得html状态码
# print(response.getcode())

#获得地址
# print(response.geturl())

#获得headers
# print(response.getheaders())

接下来就是用爬虫下载一些东西,也很简单,一个api

import urllib.request

# url_page = "http://www.baidu.com"

# # 两个参数,网址和保存的文件名
# urllib.request.urlretrieve(url_page, "baidu.html")

# 同理
# url_img = "https://p1.ssl.qhimg.com/t0188e0a4588f5bd0d3.jpg"
# urllib.request.urlretrieve(url_img, "laola.jpg")

url_video = "https://www.bilibili.com/3cc37341-d386-40c1-a882-e54f50895c03"
# urllib.request.urlretrieve(url_video, "laola.mp4")
posted @ 2023-07-07 22:34  阿基米德的澡盆  阅读(13)  评论(0编辑  收藏  举报