python获取某网站基本信息并存入数据库

该博客介绍了如何使用Python爬虫技术从指定电影网站抓取基本信息,并将数据存储到数据库中,提供了一个通用的解决方案,只需配置数据库连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现提取某电影网基本信息并存入数据库,修改数据库连接即可使用

网站:aHR0cHM6Ly93d3cuZHl0dDhhLmNvbQ==(b64加密)

代码如下:

import requests
from lxml import etree
import pymysql
from pymysql.converters import escape_string


class Mysqlmovie(object):
    def __init__(self):
        self.table = 'movie'
        self.db = pymysql.connect(
            """
            """
        )
        self.cursor = self.db.cursor()

    def process(self, item):
        movie = self.select_one(item['name'])
        if movie:
            pass
        else:
            self.insert_one(item)

    def select_one(self, moviename):
        sql = "SELECT moviename from movie where moviename = '%s';" % (escape_string(moviename))
        self.cursor.execute(sql)
        column_tuples = self.cursor.fetchall()
        columns = []
        for column_tuple in column_tuples:
            columns.append(column_tuple[0])
        return columns

    def insert_one(self, item):
        sql = "insert ignore into movie (`moviename`, `place`, `score`, `year`, `type`) " \
              "values " \
              "('%s', '%s', '%s', '%s', '%s')" % \
              (escape_string(item['name']), escape_string(item['place']), escape_string(item['score']), item['year'], escape_string(item['type']))
        self.cursor.execute(sql)
        self.db.commit()


def get_one_page(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.62',
    }
    resp = requests.get(url, headers=headers)
    resp.encoding = "utf-8"
    html = etree.HTML(resp.text)
    lis = html.xpath("/html/body/div[1]/div/div[3]/div/div/ul/li")
    for li in lis:
        item = {}
        name = li.xpath("./div/div/h4/a/text()")[-1]
        item['name'] = name
        data = li.xpath("./div/div/p/text()")[-1]
        score = li.xpath('./div/a/span[2]/span[1]/text()')[-1]
        item['score'] = score.replace('分', '')
        # print(data)
        datas = data.split('/')
        # print(datas)
        if len(datas) == 3:
            item['year'] = datas[0]
            item['place'] = datas[1]
            item['type'] = datas[2]
            item['page'] = i
            print(item)
            # print('***'*5)
            Mysqlmovie().process(item)


if __name__ == '__main__':
    for i in range(1, 10):  # 自己改页码
        url = "https://www.dytt8a.com/dytt/1-{}.html".format(i)
        print(url)
        get_one_page(url)
# 获取片名,地区,评分,年份,类型

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值