【问题标题】:How to save scraped data to a MySQL database如何将抓取的数据保存到MySQL数据库
【发布时间】:2022-11-27 14:58:52
【问题描述】:

我有一个 python 脚本,可以从求职网站上抓取数据。我想将这些抓取的数据保存到 MySQL 数据库,但在编写代码后,它连接到数据库。现在连接后,它不会创建表,因此无法将这些数据插入表中。请我需要我的代码将这些抓取的数据存储到 MYSQL 数据库中的表中。

这是我的代码

import requests
from bs4 import BeautifulSoup
import mysql.connector

for x in range(1, 210):
    html_text = requests.get(f'https://www.timesjobs.com/candidate/job-search.html? from=submit&actualTxtKeywords=Python&searchBy=0&rdoOperator=OR&searchType=personalizedSearch&luceneResultSize=25&postWeek=60&txtKeywords=Python&pDate=I&sequence={x}&startPage=1').text
    soup = BeautifulSoup(html_text, 'lxml')
    jobs = soup.find_all('li', class_ = 'clearfix job-bx wht-shd-bx')

job_list = []
for job in jobs:
     company_name = job.find('h3', class_ = 'joblist-comp-name').text.strip().replace(' ','')
     keyskill = job.find('span', class_ = 'srp-skills').text.strip().replace(' ','')
     
       
     all_detail = {company_name, keyskill}
     job_list.append(all_detail)
         
     db = mysql.connector.connect(host= 'localhost',
                         user= 'root',
                         password= 'Maxesafrica2')
     
     cursor = db.cursor()
     cursor.execute("CREATE DATABASE first_db")
     print("Connection to MYSQL Established!")

     db = mysql.connector.connect(host= 'localhost',
                     user= 'root',
                     password= 'Maxesafrica2',
                     database = 'first_db'
                    )
     
     print("Connected to Database!")
     cursor = db.cursor()
     mysql_create_table_query = """CREATE TABLE first_tbl (Company Name Varchar(300) NOT NULL,
                                                            Keyskill Varchar(400) NOT NULL)"""
                                
     result = cursor.execute(mysql_create_table_query)
     insert_query = """INSERT INTO first_tbl (Company Name, Keyskill)
                         VALUES (%s, %s)"""
                         
     records_to_insert = job_list  
     
     cursor = db.cursor()
     cursor.executemany(mysql_create_table_query, records_to_insert)                  

     db.commit()
     cursor.close()
     db.close()
     
     print('Done!')

这是我得到的错误

Connection to MYSQL Established!
Connected to Database!
Traceback (most recent call last):
File "C:\Users\LP\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 565, in cmd_query
self._cmysql.query(mysql_connector.MySQLInterfaceError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name Varchar(300) NOT NULL,
                                                ' at line 1


                
    

【问题讨论】:

    标签: python mysql


    【解决方案1】:

    列名的小问题。而不是“公司名称”,它需要是“公司名称”。 SQL 不喜欢列名中的空格。 您应该运行的更新查询:

    CREATE TABLE first_tbl 
    (
        Company_Name Varchar(300) NOT NULL,
        Keyskill Varchar(400) NOT NULL
    )
    
    INSERT INTO first_tbl 
        (Company_Name, Keyskill)
    VALUES (%s, %s)
    
    

    【讨论】:

      【解决方案2】:

      我认为这个问题与您输入的字段名称“公司名称”有关,中间有一个空格。像这样尝试使用和_的名称

      mysql_create_table_query = """CREATE TABLE first_tbl (Company_Name Varchar(300) NOT NULL,
      Keyskill Varchar(400) NOT NULL)"""
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-22
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多