定义:按照一定规则,自动抓取万维网信息的程序或脚本。
两大特征:
两大步骤
爬虫分类
Python常用的网络包
包含的模块
这个模块的作用:
打开网页,并把相应页面作为返回结果。然后可以使用read()读取网页内容。
注: 打开的网页是bytes类型的,需要使用decode("utf - 8")解码,参数为解码的格式。同时也可以安装chardet模块,它可以自动检测页面的编码格式,当然,不一定准确。
chardet的使用格式:
cs = chardet.detect(html) # 得到字典
decode(cs.get("encoding", "utf - 8")) # 自动检测编码格式并解码,如果检测不到,以默认设置解码
举例:
使用urllib包中的request模块的方法request.urlopen()打开前程无忧网的任意页面,然后将其结果打印出来。
# 导入urllib包,并引用request模块
from urllib import request
# 使用urllib.request请求一个网页内容,把内容打印出来
if __name__ == ‘__main__‘:
# 首先定义好需要访问的网站网页
url = "https://search.51job.com/list/060000%252C00,000000,0000,00,9,99,Python,2,1.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="
# 然后使用request.urlopen打开相应url并把相应页面作为返回
rsp = request.urlopen(url)
#把返回结果读取出来
# 读取出来的内容的类型为bytes字节
html = rsp.read()
# 由于该网页的编码格式为“gbk”,所以解码时也使用该格式
html = html.decode("gbk")
print(html)
结果:打印出形如以下的结果
<!DOCTYPE html>
<html>
<head>
<meta>
<title></title>
</head>
<body>
<script>
</script>
</body>
</html>
特点注意: 使用urlopen()打开的网页结果返回是的tybes类型,需要使用encode()并指定编码格式解码出来才能打印出正确的结果。
当然,在使用encode()时,每次都需要修改格式,是否有些麻烦,在这里使用chardet模块就是一个不错的选择,程序会自动检测网页的编码格式,然后以相应的编码格式打印网页。
例: 使用chardet模块检测网页编码格式
import chardet
...
html = request.urlopen(url)
# 使用chardet生成编码字典
cs = chardet.detect(html)
# 自动检测网页编码格式,然后以相应编码格式解码出来,如果没有检测到,使用默认设置格式
# 使用get取值的目的是保证不出错
html = html.decode(cs.get("encoding", "utf - 8"))
print(html)
结果还是一样的。
注意, 使用chardet.detect()生成编码格式字典,然后使用get()可以自动检测网页,其中,get()的参数表示默认格式,如果字典中没有检测出网页的格式,就以其默认设置的格式解码网页。
使用这三个方法,分别可以得到网页的url,meta信息和http code。
还是以先前的例子为例。
# 导入urllib包,并引用request模块
from urllib import request
# 使用urllib.request请求一个网页内容,把内容打印出来
if __name__ == ‘__main__‘:
# 首先定义好需要访问的网站网页
url = "https://search.51job.com/list/060000%252C00,000000,0000,00,9,99,Python,2,1.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="
# 然后使用request.urlopen打开相应url并把相应页面作为返回
rsp = request.urlopen(url)
# 返回结果网页的url
print("URL: {0}".format(rsp.geturl()))
# 得到网页的元信息,相当于HTTP头部信息
print("Info: {0}".format(rsp.info()))
# http code
print("Code: {0}".format(rsp.getcode()))
结果:
URL: https://search.51job.com/list/060000%252C00,000000,0000,00,9,99,Python,2,1.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=
Info: Date: Mon, 08 Jul 2019 10:12:44 GMT
Server: Apache
Set-Cookie: guid=61af6f9db92ec772d5fe3a15809819b2; expires=Wed, 07-Jul-2021 10:12:44 GMT; path=/; domain=.51job.com; httponly
Set-Cookie: search=jobarea%7E%60060000%7C%21; expires=Tue, 07-Jul-2020 10:12:44 GMT; path=/; domain=.51job.com; httponly
Set-Cookie: nsearch=jobarea%3D%26%7C%26ord_field%3D%26%7C%26recentSearch0%3D%26%7C%26recentSearch1%3D%26%7C%26recentSearch2%3D%26%7C%26recentSearch3%3D%26%7C%26recentSearch4%3D%26%7C%26collapse_expansion%3D; expires=Tue, 07-Jul-2020 10:12:44 GMT; path=/; domain=.51job.com; httponly
Set-Cookie: search=jobarea%7E%60060000%7C%21ord_field%7E%600%7C%21; expires