python_6_20

笔记

1、BeautifulSoup 解析库
2、MongoDB 存储库
3、requests-html 请求库

BeautifulSoup
1、什么bs4,为什么要使用bs4?
是一个基于re开发的解析库,可以提供一些强大的解析功能。
提高提取数据的效率与爬虫开发效率。

2、安装与使用
pip3 install beautifulsoup4 # 安装bs4
pip3 install lxml # 下载lxml解析器

MongoDB 非关系型数据库
一 安装与使用
1、下载安装
https://www.mongodb.com/download-center/community

2、在C盘创建一个data/db文件夹
- 数据的存放路径

3、mongod启动服务
进入终端,输入mongod启动mongoDB服务。

4、mongo进入mongoDB客户端
打开一个新的终端,输入mongo进入客户端

二 数据库操作

数据库操作:
切换库:
SQL:
use admin; 有则切换,无则报错。

    MongoDB:
    use tank; 有则切换,无则创建,并切换tank库中。

查数据库:
    SQL:
    show databases;

    MongoDB:
    show dbs;
    显示的数据库若无数据,则不显示。

删除库:
    SQL:
    drop database

    MongoDB:
    db.dropDatabase()

集合操作: MySQL中叫做表。
创建集合:
SQL:
create table f1, f2…

    MongoDB:
    # 在当前库中通过.来创建集合
    db.student

插入数据:
    # 插入多条数据
    db.student.insert([{"name1": "tank1"}, {"name2": "tank2"}])

    # 插入一条
    db.student.insert({"name": "tank"})


查数据:
    # 查找student集合中所有数据
    db.student.find({})

    # 查一条 查找name为tank的记录
    db.student.find({"name":"tank"})

三 python链接MongoDB
1、下载第三方模块pymongo
pip3 install pymongo

2、链接mongoDB客户端
client = MongoClient(‘localhost’, 27017)

from pymongo import MongoClient

# 1、链接mongoDB客户端
# 参数1: mongoDB的ip地址
# 参数2: mongoDB的端口号 默认:27017
client = MongoClient('localhost', 27017)
print(client)

# 2、进入tank_db库,没有则创建
print(client['tank_db'])

# 3、创建集合
print(client['tank_db']['people'])

# 4、给tank_db库插入数据

# 1.插入一条
data1 = {
    'name': 'tank',
    'age': 18,
    'sex': 'male'
}
client['tank_db']['people'].insert(data1)

# 2.插入多条
data1 = {
    'name': 'tank',
    'age': 18,
    'sex': 'male'
}
data2 = {
    'name': '戚志云',
    'age': 84,
    'sex': 'female'
}
data3 = {
    'name': '沈金金',
    'age': 73,
    'sex': 'male'
}
client['tank_db']['people'].insert([data1, data2, data3])

# 5、查数据
# 查看所有数据
data_s = client['tank_db']['people'].find()
print(data_s)  # <pymongo.cursor.Cursor object at 0x000002EEA6720128>
# 需要循环打印所有数据
for data in data_s:
    print(data)

# 查看一条数据
data = client['tank_db']['people'].find_one()
print(data)

# 官方推荐使用
# 插入一条insert_one
client['tank_db']['people'].insert_one()
# 插入多条insert_many
client['tank_db']['people'].insert_many()
  • 爬取豌豆荚游戏信息

'''
主页:
    图标地址、下载次数、大小、详情页地址

详情页:
    游戏名、好评率、评论数、小编点评、下载地址、简介、网友评论、1-5张截图链接地址、
https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page=1&ctoken=FRsWKgWBqMBZLdxLaK4iem9B

https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page=2&ctoken=FRsWKgWBqMBZLdxLaK4iem9B

https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page=3&ctoken=FRsWKgWBqMBZLdxLaK4iem9B

32
'''
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
'''
3、把豌豆荚爬取的数据插入mongoDB中
    - 创建一个wandoujia库
        - 把主页的数据存放一个名为index集合中
        - 把详情页的数据存放一个名为detail集合中
'''
# 连接MongoDB客户端
client = MongoClient('localhost', 27017)
# 创建或选择wandoujia库,index集合
index_col = client['wandoujia']['index']
# 创建或选择wandoujia库,detail集合
detail_col = client['wandoujia']['detail']

# 1、发送请求
def get_page(url):
    response = requests.get(url)
    return response


# 2、开始解析
# 解析详情页
def parse_detail(text):

    soup = BeautifulSoup(text, 'lxml')
    # print(soup)

    # app名称
    try:
        name = soup.find(name="span", attrs={"class": "title"}).text
    except Exception:
        # 若有异常,设置为None
        name = None
    # print(name)

    # 好评率
    try:
        love = soup.find(name='span', attrs={"class": "love"}).text

    except Exception:
        love = None
    # print(love)

    # 评论数
    try:
        commit_num = soup.find(name='a', attrs={"class": "comment-open"}).text
    except Exception:
        commit_num = None
    # print(commit_num)

    # 小编点评
    try:
        commit_content = soup.find(name='div', attrs={"class": "con"}).text
    except Exception:
        commit_content = None
    # print(commit_content)

    # app下载链接

    try:
        download_url = soup.find(name='a', attrs={"class": "normal-dl-btn"}).attrs['href']
    except Exception:
        # 若有异常,设置为None
        download_url = None

    # print(download_url)

    # print(
    #     f'''
    #     ============= tank ==============
    #     app名称:{name}
    #     好评率: {love}
    #     评论数: {commit_num}
    #     小编点评: {commit_content}
    #     app下载链接: {download_url}
    #     ============= end ==============
    #     '''
    # )

    # 判断所有数据都存在,正常赋值
    if name and love and commit_num and commit_content and download_url :
        detail_data = {
            'name': name,
            'love': love,
            'commit_num': commit_num,
            'commit_content': commit_content,
            'download_url': download_url
        }

    # 若love没有值,则设置为 没人点赞,很惨
    if not love:
        detail_data = {
            'name': name,
            'love': "没人点赞,很惨",
            'commit_num': commit_num,
            'commit_content': commit_content,
            'download_url': download_url
        }
    # 若download_url没有值,则设置为 没有安装包
    if not download_url:
        detail_data = {
            'name': name,
            'love': love,
            'commit_num': commit_num,
            'commit_content': commit_content,
            'download_url': '没有安装包'
        }



    # 插入详情页数据
    detail_col.insert(detail_data)
    print(f'{name}app数据插入成功!')

# 解析主页
def parse_index(data):
    soup = BeautifulSoup(data, 'lxml')

    # 获取所有app的li标签
    app_list = soup.find_all(name='li', attrs={"class": "card"})
    for app in app_list:
        # print(app)
        # print('tank' * 1000)
        # print('tank *' * 1000)
        # print(app)
        # 图标地址
        # 获取第一个img标签中的data-original属性
        img = app.find(name='img').attrs['data-original']
        # print(img)

        # 下载次数
        # 获取class为install-count的span标签中的文本
        down_num = app.find(name='span', attrs={"class": "install-count"}).text
        # print(down_num)

        import re
        # 大小
        # 根据文本正则获取到文本中包含 数字 + MB(\d+代表数字)的span标签中的文本
        size = soup.find(name='span', text=re.compile("\d+MB")).text
        # print(size)

        # 详情页地址
        # 获取class为detail-check-btn的a标签中的href属性
        # detail_url = soup.find(name='a', attrs={"class": "name"}).attrs['href']
        # print(detail_url)

        # 详情页地址
        detail_url = app.find(name='a').attrs['href']
        # print(detail_url)

        # 拼接数据
        index_data = {
            'img': img,
            'down_num': down_num,
            'size': size,
            'detail_url': detail_url
        }

        # 插入数据
        index_col.insert(index_data)
        print('主页数据插入成功!')

        # 3、往app详情页发送请求
        response = get_page(detail_url)

        # 4、解析app详情页
        parse_detail(response.text)


def main():
    for line in range(1, 33):
        url = f"https://www.wandoujia.com/wdjweb/api/category/more?catId=6001&subCatId=0&page={line}&ctoken=FRsWKgWBqMBZLdxLaK4iem9B"

        # 1、往app接口发送请求
        response = get_page(url)
        # print(response.text)
        print('*' * 1000)
        # 反序列化为字典
        data = response.json()

        # 获取接口中app标签数据
        app_li = data['data']['content']
        # print(app_li)

        # 2、解析app标签数据
        parse_index(app_li)

        # 执行完所有函数关闭mongoDB客户端
        client.close()

if __name__ == '__main__':
    main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Python类,用于生成四则运算练习的题目和答案: ```python import random class FourOperations: def __init__(self, num_range=(1,10)): self.num_range = num_range def generate_expression(self, num=2): nums = [str(random.randint(*self.num_range)) for i in range(num)] operator = random.choice(["+", "-", "*", "/"]) expression = " ".join(nums) + " " + operator + " " + nums[-1] return expression def generate_exercises(self, num=10): exercises = [] for i in range(num): exp = self.generate_expression(random.randint(2,4)) exercises.append(exp) return exercises def calculate_answer(self, expression): return eval(expression) def check_answer(self, expression, answer): return self.calculate_answer(expression) == answer def generate_answers(self, expressions): answers = [] for exp in expressions: ans = self.calculate_answer(exp) answers.append(ans) return answers ``` 这个类可以生成指定数量的题目,每个题目包含2-4个随机数字和一个随机运算符。可以通过调用 `generate_expression()` 方法生成单个表达式,或者调用 `generate_exercises()` 生成多个表达式。 可以通过调用 `calculate_answer()` 方法计算给定表达式的答案,并使用 `check_answer()` 方法检查一个给定的答案是否正确。可以使用 `generate_answers()` 方法批量计算一组表达式的答案。 例如,下面是一个使用示例: ```python # 初始化一个FourOperations对象 four_ops = FourOperations(num_range=(1, 20)) # 生成10个练习题 exercises = four_ops.generate_exercises(num=10) print("Exercises:") for e in exercises: print(e) # 计算所有练习题的答案 answers = four_ops.generate_answers(exercises) # 检查答案是否正确 score = 0 for i in range(10): ans = int(input(f"Please input the answer for question {i+1}: ")) if four_ops.check_answer(exercises[i], ans): print("Correct!") score += 1 else: print("Wrong answer!") print(f"Your score is {score}/10") ``` 输出结果可能如下: ``` Exercises: 2 + 7 * 4 8 - 16 / 8 4 * 4 - 7 5 / 1 + 3 2 * 18 - 7 20 / 5 + 11 / 11 16 / 2 - 2 * 2 6 / 3 + 9 12 - 16 / 8 5 + 3 / 3 Please input the answer for question 1: 30 Correct! Please input the answer for question 2: -6 Correct! Please input the answer for question 3: 9 Correct! Please input the answer for question 4: 8 Correct! Please input the answer for question 5: 29 Correct! Please input the answer for question 6: 2 Correct! Please input the answer for question 7: 6 Correct! Please input the answer for question 8: 11 Wrong answer! Please input the answer for question 9: 10 Correct! Please input the answer for question 10: 6 Correct! Your score is 9/10 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值