使用response组件爬取网页信息

一、使用response组件爬取网页信息
1、抓取json数据格式的网站信息
2、根据指定的记录数和页数进行爬取
3、示例:
import urllib.request
import os

dburl = ‘https://movie.douban.com/typerank?type_name=%E5%89%A7%E6%83%85&type=11&interval_id=100:90&action=’
headers = {‘User-Agent’:‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36’}

start和limit可以自己随便设置

formdata = {‘start’:‘20’,‘limit’:‘100’}

#将字典转换为查询的字符串
querydata=urllib.parse.urlencode(formdata)
print(querydata)
#传递带有参数的请求信息
request=urllib.request.Request(dburl,data=querydata,headers=headers)

#打开请求对象
html=urllib.request.urlopen(request)

print(html.read())

4、爬取网站的基本步骤
(1) 请求网址信息–对服务器资源发起请求
(2) 服务器响应客户端请求的信息
(3) 客户端对服务器返回的信息进行解析
(4) 保存响应的数据到文件或者数据库中

二、使用requests库爬取网页内容
1、requests库是由python编写的
2、常用方法
(1) 获取网站的请求
htmlstr=requests.get(url地址) 以get请求方式获取网站响应的数据
htmlstr=requests.post(url地址) 以post请求方式获取网站响应的数据

(2) 将请求的信息转为字符串(支持utf-8编码)
strxxx=htmlstr.text

(3) 示例如下
import requests
headers = {
‘User-Agent’:‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36’
}

urlstr=“http://news.sohu.com/”
html=requests.get(urlstr,headers=headers,verify=False)
print(html.text)

三、使用BeautifulSoup按照指定标签爬取信息
1、安装beautifulSoup4组件(支持python3.x版本)
2、在项目中应用此组件
From bs4 import BeautifulSoup
3、主要用于对标签元素的内容和属性进行解析处理
4、需要和urllib以及requests库联合使用

5、使用方式如下:
from bs4 import BeautifulSoup
import urllib.request
headers = {
‘User-Agent’:‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36’
}
#先获取整个网页数据
urlstr=“http://bbs.tianya.cn/list-free-1.shtml”
request=urllib.request.Request(urlstr,headers=headers)
html=urllib.request.urlopen(request)
#创建BeautifulSoup对象
bs=BeautifulSoup(html,“html.parser”)
print(type(bs.title))
print(bs.title.text)

6、使用bs对象获取Tag标签元素对象
tag对象=bs.标签名称
(1) Tag标签对象的常用方法
from bs4 import BeautifulSoup
import urllib.request
headers = {
‘User-Agent’:‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36’
}
#先获取整个网页数据
urlstr=“http://bbs.tianya.cn/list-free-1.shtml”
request=urllib.request.Request(urlstr,headers=headers)
html=urllib.request.urlopen(request)
#创建BeautifulSoup对象
bs=BeautifulSoup(html,“html.parser”)
print(type(bs.title))
print(bs.title.text)

#获取所有的tr标签对象
mytr=bs.tr
print(type(mytr))
mytds=bs.td
print(type(mytds))
for mytd in mytds:
print(mytd)

(2) 使用标签获取属性对象attrs(Attributes)
获取所有a标签的属性
myaaattrs=bs.a.attrs
print(“输出所有a标签的属性:”)
print(myaaattrs)

效果如下:
输出所有a标签的属性:
{’_tystat’: ‘新版顶导航/Logo’, ‘href’: ‘https://bbs.tianya.cn/’}

获取tr的指定标签的属性
例如获取tr标签的class属性
bs.tr[‘class’]

(3) Tag的contents属性遍历节点数
from bs4 import BeautifulSoup
import urllib.request
headers = {
‘User-Agent’:‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36’
}
#先获取整个网页数据
urlstr=“http://bbs.tianya.cn/list-free-1.shtml”
request=urllib.request.Request(urlstr,headers=headers)
html=urllib.request.urlopen(request)
#创建BeautifulSoup对象
bs=BeautifulSoup(html,“html.parser”)

mybody=bs.body
#获取mybody的contents属性值
#print(mybody.contents)
for myelement in mybody.contents:
print(myelement)

四、查找文档树指定的节点集合
1、使用find_all方法进行查询
例如查找所有的tr标签
语法
Taglist=bs.find_all(‘标签名称’,attrs=”{‘属性’:’值’,…}”)
示例,查找所有的tr标签元素对象
Mytr=bs.find_all(“tr”)

示例如下:
from bs4 import BeautifulSoup
import urllib.request
headers = {
‘User-Agent’:‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36’
}
#先获取整个网页数据
urlstr=“http://bbs.tianya.cn/list-free-1.shtml”
request=urllib.request.Request(urlstr,headers=headers)
html=urllib.request.urlopen(request)
#创建BeautifulSoup对象
bs=BeautifulSoup(html,“html.parser”)

tdlist=bs.find_all(“td”,class_=“td-title faceblue”)
print(type(tdlist))
for mytd in tdlist:
#print(mytd)#遍历所有的td标签
print(mytd.text.strip())

上一篇:ORACLE数据库日常维护手册(最全+最实用)


下一篇:转换 Byte 数组到 ... - 回复 "高群" 的问题