# hot_score = spot.find("b", class_="hot_score_number").text.strip() or 0 if spot.find("li", class_="").find("a", class_="score").text.strip() == "暂无评分": score = spot.find("li", class_="").find("a", class_="score").text.strip() else: score = spot.find("li", class_="").find("strong").text.strip() address = spot.find("dd", class_="ellipsis").text.strip() col.insert_one({"name": name, "hot_score": hot_score, "score": score, "address": address}) print({"名称": name, "热度": hot_score, "评分": score, "地址": address}) if __name__ == "__main__": get_scenic_spots()
时间: 2023-07-24 08:17:24 浏览: 42
这段代码是在之前的基础上,将获取到的景点名称、热度评分、评分和地址存入MongoDB数据库,并将其打印出来。具体实现过程是:首先,通过判断"hot_score_number"标签是否存在来决定热度评分的值,并将其赋值给hot_score变量;之后,通过判断评分是否为"暂无评分"来确定score的值,并将其赋值给score变量;最后,通过find方法找到class属性为"ellipsis"的dd标签,再使用text和strip方法将文本内容提取出来,并赋值给变量address。接着,通过insert_one方法将获取到的数据存入MongoDB数据库中,并将其打印出来。需要注意的是,该代码片段中的col是数据库中的一个集合,需要根据实际情况进行修改。
相关问题
for spot in spot_list: name = spot.find("dt", class_="").find("a", class_="").text.strip() if spot.find("b", class_="hot_score_number"): hot_score = spot.find("b", class_="hot_score_number").text.strip() else: hot_score = 0
这段代码是在之前的基础上,进一步从每个景点的div标签中获取景点名称和热度评分。具体实现过程是:通过find方法找到class属性为空的dt标签,再通过find方法找到class属性为空的a标签,最后使用text和strip方法将文本内容提取出来,并赋值给变量name;之后,通过判断是否存在class属性为"hot_score_number"的b标签来确定该景点是否有热度评分,如果有就通过find方法获取文本内容并去除首尾空格,否则将热度评分赋值为0。需要注意的是,该代码片段仅仅获取了每个景点的名称和热度评分信息,如果需要获取更多信息,还需要继续编写代码。
# One Hot Encodes one_hot_cols = dataset_bin.columns.tolist() #one_hot_cols.remove('MathScore') #one_hot_cols.remove('ReadingScore') #one_hot_cols.remove('WritingScore') one_hot_cols.remove('average') dataset_bin_enc = pd.get_dummies(dataset_bin, columns=one_hot_cols) print(dataset_bin_enc) dataset_bin_enc.head()features=dataset_bin_enc.loc[:,"MathScore_(-0.1, 10.0]":"WritingScore_(90.4, 100.0]"] y=features.values X=dataset_bin_enc["average"].values print(X) print(y) type(X1)import numpy as np #导入画图工具 import matplotlib.pyplot as plt
这段代码的作用是将数据集中的某些列进行 one-hot 编码,然后将编码后的特征作为 X,将平均成绩作为 y,最终得到 X 和 y 的值。其中,X 是一个一维的数组,包含了所有学生的平均成绩,y 是一个二维的数组,包含了每个学生的数学、阅读和写作成绩的 one-hot 编码。
最后,代码导入了 numpy 和 matplotlib.pyplot 两个库,用于后续的数据处理和可视化。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)