Python之爬虫

187 阅读7分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

py爬虫

1.requests模块

1.1作用:

模拟浏览器发送请求

1.2如何使用:

指定url,发起请求,获取响应数据,持久化存储

1.3环境安装

pip install request

1.4编码

需求:通过requests爬取搜狗首页的页面数据

import requests
​
# 爬取百度首页的源码if __name__ == "__main__":
    url = "https://www.baidu.com"
    response = requests.get(url)
    print(response)
​
    baidu_page = response.text
    print(baidu_page)
​
    with open("./baiduPage.html", 'w', encoding='utf-8') as fp:
        fp.write(baidu_page)
    print("爬取完毕!")
​
  • 巩固
  1. 爬取百度词条对应的搜索结果页面 → UA伪装
  2. 破解百度翻译 → 获取页面中局部的数据,post请求,ajax请求,响应数据是一组json数据
  3. 爬取豆瓣电影分类排行榜 →
  4. 爬取肯德基餐厅查询
  5. 爬取国家药品监督管理总局中基于中华人民共和国化妆品生产许可证相关数据

豆瓣例子

1.项目显式入口

def print_hi(name):
    # 在下面的代码行中使用断点来调试脚本。
    print(f'Hi, {name}')  # 按 Ctrl+F8 切换断点。
​
​
# 程序的显式入口
if __name__ == '__main__':
    print_hi('PyCharm')

2.引入包

import re               # 正则表达式,文字匹配
import bs4              # 数据获取进行网页解析
import urllib.request,urllib.error          # 指定url,获取网页
import xlwt             # 进行excel操作
import sqlite3          # SQLite数据库操作

3.模拟用户正常登录的简单情况

# 获取一个post请求
# 模拟用户正常登录的情况
data = bytes(urllib.parse.urlencode({"hello": "world"}), encoding="utf-8")
response = urllib.request.urlopen("http://httpbin.org/post", data=data)
print(response.read().decode('utf-8'))
print(response.getcode())   # 获取状态码
print(response.status)      # 获取状态码

4.超时处理

# 获取一个get请求
# 设置超时时间限制,try-catch捕获异常
try:
    response = urllib.request.urlopen("http://httpbin.org/get",timeout=0.0001)
    print(response.read().decode('utf-8'))
except urllib.error.URLError as e:
    print("time out!")

5.获取头部信息

# 模拟服务器发送消息获取的头部信息
response = urllib.request.urlopen("http://httpbin.org/get")
print(response.status)
print(response.getheaders())
print(response.getheader("Content-Type"))

6.用户代理,隐藏user-agent信息

# 伪装成为机器访问,隐藏爬虫信息
# 构建请求对象
url = "http://httpbin.org/post"
data = bytes(urllib.parse.urlencode({"name": "mary"}),encoding='utf-8')
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.3"
}
req = urllib.request.Request(url=url,data=data,headers=headers,method="POST")      # 封装数据# 请求对象发送请求
response = urllib.request.urlopen(req)
​
# 打印对象发送请求的信息
print(response.read().decode("utf-8"))

7.通过函数的使用爬取一个页面

from bs4 import BeautifulSoup       # 网页解析
import re
import urllib.error,urllib.request
import sqlite3
import xlwt
​
​
def main():
    baseurl = "https://movie.douban.com/top250?start="
    # 步骤:1.网页   2.逐个解析数据   3.保存
    datalist = getData(baseurl)
​
    savePath = '豆瓣电影Top250.xls'
    saveData(datalist, savePath)
​
​
# 爬取网页,获取数据,返回数据列表
def getData(baseurl):
    datalist = []
    # 2.获取数据前逐一解析数据
    return datalist
​
​
# 得到指定URL的网页内容
def askURL(url):
    # 用户代理,伪装浏览器头部信息
    header = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
    }
    req = urllib.request.Request(url=url,headers=header)
    html = ""
    try:
        response = urllib.request.urlopen(req)
        html = response.read().decode("utf-8")
        print(html)
    except urllib.error.URLError as e:
        if hasattr(e,"code"):
            print(e.code)
        if hasattr(e,"reason"):
            print(e.reason)
        if hasattr(e,"filename"):
            print(e.filename)
​
​
# 保存数据
def saveData(savePath):
    # 3.保存数据
    print(savePath)
​
​
# 程序的显式入口
if __name__ == '__main__':
    main()

8.bs4的使用

8.1遍历文档树

.contents:获取Tag的所有子节点,返回一个list
.children:获取Tag的所有子节点,返回一个生成器
.descendants:获取Tag的所有子孙节点
.strings:如果Tag包含多个字符串,即在咨询节点中有内容,可以用此获取,而后遍历
.parent:获取Tag的父节点
.stripped_strings:与strings用法一致,但可以去除多余的空白内容

需要使用时搜索BeautifulSoup文档

# Tag:拿到它所找到的第一个内容
file = open("./top250.html", "rb")       # readbytes
html = file.read().decode('utf-8')
bs = BeautifulSoup(html, "html.parser")  # BeautifulSoup使用html.parser解析变量名为html获取的文件# 文档的遍历
print(bs.head.contents)
print(bs.head.contents[1])
​
# 文档的搜索
# 1.find_all()查找包含字母里面的内容
a_list = bs.find_all("a")
print(a_list)
​
​
# 2.正则表达式搜索
div_list = bs.find_all(re.compile("div"))
print(div_list)
​
​
# 3.语句搜索
def name_is_exists(tag):
    return tag.has_attr("name")
​
t_list = bs.find_all(name_is_exists)
for item in t_list:
    print(item)
print(t_list)
​
​
# 4.kwargs 参数
lists = bs.find_all(id="suggResult")        # 查找id=suggResult的所有标签
for i in lists:
    print(i)
​
​
class_lists = bs.find_all(class_=True)      # 查找class有命名的所有标签
for i in class_lists:
    print(i)
​

9.用7.中爬取到的页面进行bs4解析

# 定义全局的正则表达式
# 获取影片详情链接规则
findLink = re.compile(r'<a href="(.*?)">')  # 创建规则
# 获取影片图片链接规则
findImgSrc = re.compile(r'<img.*src="(.*?)">', re.S)  # re.S 忽略换行符,包含换行符
# 获取影片片名的规则
findTitle = re.compile(r'<span class="title">(.*)</span>')
# 获取影片评分的规则
findRating = re.compile(r'<span class="rating_num" property="v:average">(.*)</span>')
# 找到评价人数
findJudge = re.compile(r'<span>(\d*)人评价</span>')
# 找到概况
findInq = re.compile(r'<span class="inq">(.*)</span>')
# 找到影片的相关内容
findBd = re.compile(r'<p class="">(.*?)</p>', re.S)
​
​
# 爬取网页,获取数据,返回数据列表
def getData(baseurl):
    datalist = []
​
    # 爬取多个页面
    for i in range(0, 10):  # 调用获取页面信息的函数10次,1次25个
        url = baseurl + str(i * 25)
        html = askURL(url)  # 每次保存,都要获取网页源码
​
        # 2.每次获取数据前,都要逐一解析数据
        soup = BeautifulSoup(html, "html.parser")
        for i in soup.find_all("div", class_="item"):
            # print(i)  # 测试查看电影的全部信息,print()这种打印的几乎都是测试
            data = []  # 保存一部电影的所有信息
            item = str(i)
​
            # 匹配获取影片的规则,来获取影片详情的链接
            link = re.findall(findLink, item)[0]  # re库通过正则表达式查找指定的字符串
            # print(link)   # 测试影片链接
            data.append(link)  # 添加链接
            imgSrc = re.findall(findImgSrc, item)[0]
            data.append(imgSrc)  # 添加图片
            titles = re.findall(findTitle, item)
            # 片名只有一个中文名时,没有外国名
            if len(titles) == 2:
                ctitle = titles[0]
                data.append(ctitle)  # 添加中文名
                otitle = titles[1].replace("/", "")  # 去掉无关的符号
                data.append(otitle)  # 添加外国名
            else:
                data.append(titles[0])
                data.append(' ')  # 外国名留空
​
            rating = re.findall(findRating, item)[0]
            data.append(rating)
            judgeNum = re.findall(findJudge, item)[0]
            data.append(judgeNum)
            inq = re.findall(findInq, item)
            if len(inq) != 0:
                inq = inq[0].replace("。", "")
                data.append(inq)
            else:
                data.append(" ")  # 留空、
​
            bd = re.findall(findBd, item)[0]
            bd = re.sub('<br(\s+)?/>(\s+)?', " ", bd)  # 去掉<br/>
            bd = re.sub('/', " ", bd)  # 替换/
            data.append(bd.strip())  # 去掉前后空格
​
            datalist.append(data)  # 把处理好的一部电影信息放入datalist
    # print(datalist)   # 测试打印
    return datalist
​
​

10.保存数据

10.1保存数据到excel

import xlwt
​
workbook = xlwt.Workbook(encoding="utf-8")      # 创建workbook对象,excel文件
worksheet = workbook.add_sheet('sheet01')       # 文件里面的一张sheet
worksheet.write(0,0,'hello')                    # 写入数据,第一个参数为’行‘,第二个参数为’列‘,第三个参数为数据内容
worksheet.save('student.xls')                   # 保存数据到excel
# 九九乘法表
workbook02 = xlwt.Workbook(encoding='utf-8')
worksheet02 = workbook02.add_sheet('sheet02')
for i in range(0,9):
    for j in range(0,i+1):
        worksheet02.write(i,j,"%d*%d=%d"%(j+1,i+1,(j+1)*(j+1)))
workbook02.save('九九乘法表.xls')
# 保存数据
def saveData(datalist, savePath):
    # 3.保存数据
    book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    sheet = book.add_sheet('豆瓣电影Top250', cell_overwrite_ok=True)
    col = ("影片详情链接", "影片图片链接", "影片中文名", "影片外国名", "影片评分", "评价人数", "概况", "影片相关内容")  # 元组,行
    for i in range(0, 8):
        sheet.write(0, i, col[i])  # 列名
    for i in range(0, 250):
        print("第%d条" % (i + 1))
        data = datalist[i]
        for j in range(0, 8):
            sheet.write(i + 1, j, data[j])
​
    book.save(savePath)

10.2保存数据到SQLite

import sqlite3
​
​
# 1.连接数据库
conn = sqlite3.connect("test.db")
print("opened database successfully!")
​
​
# 2.创建数据表
conn = sqlite3.connect("test.db")
print("成功打开数据库")
​
c = conn.cursor()           # 获取游标
sql = '''
    create table company
        (id int primary key not null,
        name text not null,
        age int not null,
        address char(50),
        salary real);
'''
c.execute(sql)
​
conn.commit()   # 提交数据库操作
conn.close()    # 关闭数据库连接
print("成功建表")
​
​
# 3.进行增删改查操作
# 3.1插入数据
conn = sqlite3.connect("test.db")
c = conn.cursor()   # 获取游标
sql1 = '''
    insert into company(id,name,age,address,salary)
    values(1,'张三',32,'成都',8000),(2,'李四',28,'四川',9000);
'''
sql2 = '''
    insert into company(id,name,age,address,salary)
    values(3,'王五',30,'北京',15000);
'''
c.execute(sql1)      # 执行sql语句
c.execute(sql2)
​
conn.commit()      # 提交数据库操作
conn.close()       # 关闭数据库连接
print("成功插入数据!")
​
# 3.2查询数据
conn = sqlite3.connect("test.db")
c = conn.cursor()   # 获取游标
sql1 = '''
    select id,name,address,salary from company
'''
​
cur = c.execute(sql1)     # 执行sql语句
for row in cur:
    print("id = ",row[0])
    print("name = ",row[1])
    print("address = ",row[2])
    print("salary = ",row[3],"\n")
​
conn.close()        # 关闭数据库连接
print("查询数据成功!")
# 保存数据到SQLite
def saveDataToDB(datalist, dbPath):
    init_db(dbPath)
    conn = sqlite3.connect(dbPath)
    c = conn.cursor()
​
    for data in datalist:
        for index in range(len(data)):
            data[index] = '"' + data[index] + '"'
            sql = '''
                insert into movie250(
                info_link,pic_link,cname,ename,score,rated,instroduction,info)
                values(%s)
            '''%",".join(data)
            c.execute(sql)
            conn.commit()
    c.close()
    conn.close()