鱼C论坛

 找回密码
 立即注册
查看: 2412|回复: 6

求助!!!!!! 为啥mysql 里没有数据????不知道哪错了~~~

[复制链接]
发表于 2022-4-13 23:33:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# 导入支持库
from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
import re
import pymysql
import time

# 设置Request对象
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36"
}

# 数据库连接
connect = pymysql.Connect(
    host='localhost',
    port=3306,
    user='root',
    password='',
    db='test_spider',
    charset='utf8'
)
# 获取游标
cursor = connect.cursor()
# 执行插入语句
sql_in = '''insert into doubanmovie (name,director,actor,style,country,release_time,time,score) value (%s,%s,%s,%s,%s,%s,%s,%s)'''


def get_movie_url(url):  # 获取每个电影的链接
    html = requests.get(url=url, headers=headers)
    selector = etree.HTML(html.text)
    movie_hrefs = selector.xpath('//div[@class="hd"]/a/@href')
    for movie_href in movie_hrefs:
        get_movie_info(movie_href)


def get_movie_info(url):
    html = requests.get(url=url, headers=headers)  # <Response [200]>
    selector = etree.HTML(html.text)  # <Element html at 0x20892e10108>
    try:
        name = selector.xpath('//*[@id="content"]/h1/span[1]/text()')[0]
    except IndexError:
        name = ''
    try:
        director = selector.xpath('//*[@id="info"]/span[1]/span[2]/a/text()')[0]
    except IndexError:
        director = ''
    try:
        actors = selector.xpath('//*[@class="actor"]/span[2]')[0]
        actor = actors.xpath('string(.)')
        # actors = re.findall('<a href="/.*?/" rel="v:starring">(.*?)</a>',html.text,re.S)
    except IndexError:
        actor = ''
    try:
        style = re.findall('<span property="v:genre">(.*?)</span>', html.text, re.S)[0]
    except IndexError:
        style = ''
    try:
        country = re.findall('<span class="pl">制片国家/地区:</span> (.*?)<br/>', html.text, re.S)[0]
    except IndexError:
        country = ''
    try:
        release_time = re.findall('<span property="v:initialReleaseDate" content=.*?>(.*?)</span>', html.text, re.S)[0]
    except IndexError:
        release_time = ''
    try:
        time = re.findall('<span property="v:runtime" content=.*?>(.*?)</span>', html.text, re.S)[0]
    except IndexError:
        time = ''
    try:
        score = selector.xpath('//*[@id="interest_sectl"]/div[1]/div[2]/strong/text()')[0]
    except IndexError:
        score = ''
    cursor.execute(
        sql_in,
        [str(name), str(director), str(actor), str(style), str(country), str(release_time), str(time), str(score)]
    )

    urls = ['https://movie.douban.com/top250?start={}&filter='.format(i) for i in range(0, 250, 25)]

    for url in urls:
        get_movie_url(url)
        time.sleep(5)
        print('我好了!')
    connect.commit()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-14 06:44:25 From FishC Mobile | 显示全部楼层
在写入数据库之前先print一下你的数据
盲猜你根本就没取到数据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-14 08:09:21 | 显示全部楼层
print大法看看拿没拿到啊,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-14 19:32:21 | 显示全部楼层
有老铁帮忙实现一下吗  py小白
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-14 19:33:18 | 显示全部楼层
wp231957 发表于 2022-4-14 06:44
在写入数据库之前先print一下你的数据
盲猜你根本就没取到数据

py小白 难受
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-14 19:40:32 From FishC Mobile | 显示全部楼层
shiey 发表于 2022-4-14 19:33
py小白  难受

不知道你的代码是打哪来的,要知道单单是数据库就够写一本书的了
你这爬虫也不知会否,数据库也不知会否
就弄出这么复杂的代码,狠是佩服你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-14 19:43:21 | 显示全部楼层

  1. # 导入支持库
  2. from urllib.request import urlopen, Request
  3. from bs4 import BeautifulSoup
  4. from lxml import etree                             # 需要从 lxml 模块 导入 etree
  5. import re
  6. import requests                           # 需要导入 requests 模块
  7. import pymysql
  8. import time

  9. # 设置Request对象
  10. headers = {
  11.     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36"
  12. }

  13. # 数据库连接
  14. connect = pymysql.Connect(
  15.     host='localhost',
  16.     port=3306,
  17.     user='root',
  18.     password='secureworks',
  19.     db='test_spider',
  20.     charset='utf8'
  21. )
  22. # 获取游标
  23. cursor = connect.cursor()
  24. # 执行插入语句
  25. sql_in = '''insert into doubanmovie (name,director,actor,style,country,release_time,time,score) value (%s,%s,%s,%s,%s,%s,%s,%s)'''


  26. def get_movie_url(url):  # 获取每个电影的链接
  27.     html = requests.get(url=url, headers=headers)
  28.     selector = etree.HTML(html.text)
  29.     movie_hrefs = selector.xpath('//div[@class="hd"]/a/@href')
  30.     for movie_href in movie_hrefs:
  31.         get_movie_info(movie_href)


  32. def get_movie_info(url):
  33.     html = requests.get(url=url, headers=headers)  # <Response [200]>
  34.     selector = etree.HTML(html.text)  # <Element html at 0x20892e10108>
  35.     try:
  36.         name = selector.xpath('//*[@id="content"]/h1/span[1]/text()')[0]
  37.     except IndexError:
  38.         name = ''
  39.     try:
  40.         director = selector.xpath('//*[@id="info"]/span[1]/span[2]/a/text()')[0]
  41.     except IndexError:
  42.         director = ''
  43.     try:
  44.         actors = selector.xpath('//*[@class="actor"]/span[2]')[0]
  45.         actor = actors.xpath('string(.)')
  46.         # actors = re.findall('<a href="/.*?/" rel="v:starring">(.*?)</a>',html.text,re.S)
  47.     except IndexError:
  48.         actor = ''
  49.     try:
  50.         style = re.findall('<span property="v:genre">(.*?)</span>', html.text, re.S)[0]
  51.     except IndexError:
  52.         style = ''
  53.     try:
  54.         country = re.findall('<span class="pl">制片国家/地区:</span> (.*?)<br/>', html.text, re.S)[0]
  55.     except IndexError:
  56.         country = ''
  57.     try:
  58.         release_time = re.findall('<span property="v:initialReleaseDate" content=.*?>(.*?)</span>', html.text, re.S)[0]
  59.     except IndexError:
  60.         release_time = ''
  61.     try:
  62.         time = re.findall('<span property="v:runtime" content=.*?>(.*?)</span>', html.text, re.S)[0]
  63.     except IndexError:
  64.         time = ''
  65.     try:
  66.         score = selector.xpath('//*[@id="interest_sectl"]/div[1]/div[2]/strong/text()')[0]
  67.     except IndexError:
  68.         score = ''
  69.     cursor.execute(
  70.         sql_in,
  71.         [str(name), str(director), str(actor), str(style), str(country), str(release_time), str(time), str(score)]
  72.     )

  73. '''这一部分代码缩进错误'''
  74. urls = ['https://movie.douban.com/top250?start={}&filter='.format(i) for i in range(0, 250, 25)]

  75. for url in urls:
  76.     get_movie_url(url)
  77.     time.sleep(5)
  78.     print('我好了!')
  79. connect.commit()
  80. '''这一部分代码缩进错误'''
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 19:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表