如何用 Python 的 Beautiful Soap 创建 MySQL 数据库来存储 Stack Overflow 问题

对于 Stack Overflow 的问题进行分析和研究需要大量的数据,因此将其存储在数据库中非常有必要。本文将介绍如何使用 Python 的 Beautiful Soap 库从 Stack Overflow 网站抓取数据,并将其存储在 MySQL 数据库中。

2、解决方案

1)获取 Stack Overflow 问题数据

首先,我们需要使用 Beautiful Soap 库从 Stack Overflow 网站抓取问题数据。可以通过以下步骤进行:

  1. 安装 Beautiful Soup 库。可以运行以下命令进行安装:
pip install beautifulsoup4
  1. 导入 Beautiful Soup 库。在 Python 脚本中,可以使用以下代码导入该库:
from bs4 import BeautifulSoup
  1. 使用 Beautiful Soup 库解析 Stack Overflow 网页。可以使用以下代码解析 Stack Overflow 网页:
soup = BeautifulSoup(html_content, "html.parser")

其中,html_content 是 Stack Overflow 网页的 HTML 内容。

  1. 从解析后的 Stack Overflow 网页中提取问题数据。可以使用以下代码提取问题数据:
questions = soup.find_all("div", class_="question-summary")

for question in questions:
    question_id = question.find("div", class_="question-hyperlink").find("a")["id"]
    question_time = question.find("div", class_="relativetime").text
    comments = question.find_all("div", class_="comment")

    for comment in comments:
        comment_id = comment.find("a")["id"]
        comment_time = comment.find("span", class_="relativetime").text
        user_id = comment.find("div", class_="user-details").find("a")["href"].split("/")[-1]
        user_name = comment.find("div", class_="user-details").find("a").text

        # 将提取到的数据存储到数据库中

2)创建 MySQL 数据库

接下来,我们需要创建一个 MySQL 数据库来存储 Stack Overflow 问题数据。可以通过以下步骤进行:

  1. 连接到 MySQL 服务器。可以使用以下代码连接到 MySQL 服务器:
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="username",
  password="password",
  database="stack_overflow_questions"
)

其中,host 是 MySQL 服务器的地址,user 是 MySQL 用户名,password 是 MySQL 密码,database 是要创建的数据库的名称。

  1. 创建表。可以使用以下代码创建表:
cursor = mydb.cursor()

cursor.execute("CREATE TABLE questions (question_id VARCHAR(255) PRIMARY KEY, question_time DATETIME, comments INT)")
cursor.execute("CREATE TABLE comments (comment_id VARCHAR(255) PRIMARY KEY, comment_time DATETIME, user_id VARCHAR(255), user_name VARCHAR(255))")

mydb.commit()

其中,questions 表存储问题数据,comments 表存储评论数据。

3)将数据插入到 MySQL 数据库

最后,我们需要将抓取到的 Stack Overflow 问题数据插入到 MySQL 数据库中。可以使用以下代码进行:

cursor = mydb.cursor()

for question in questions:
    question_id = question.find("div", class_="question-hyperlink").find("a")["id"]
    question_time = question.find("div", class_="relativetime").text
    comments = question.find_all("div", class_="comment")

    cursor.execute("INSERT INTO questions (question_id, question_time, comments) VALUES (%s, %s, %s)", (question_id, question_time, len(comments)))

    for comment in comments:
        comment_id = comment.find("a")["id"]
        comment_time = comment.find("span", class_="relativetime").text
        user_id = comment.find("div", class_="user-details").find("a")["href"].split("/")[-1]
        user_name = comment.find("div", class_="user-details").find("a").text

        cursor.execute("INSERT INTO comments (comment_id, comment_time, user_id, user_name) VALUES (%s, %s, %s, %s)", (comment_id, comment_time, user_id, user_name))

mydb.commit()

这样,我们就成功地将 Stack Overflow 问题数据存储到了 MySQL 数据库中。

猜你喜欢

转载自blog.csdn.net/D0126_/article/details/143573775
今日推荐