weixin_46887967 2020-11-12 10:03 采纳率: 67.9%
浏览 7
已结题

这段代码没有报错,但是最后MySQL数据库里的表示空的,想问各位前辈应该怎么办?

import requests
from lxml import etree
import re
import pymysql
import time

conn = pymysql.connect(host = 'localhost',user = 'root',passwd = '52xzy1314@',db = 'mydb',port = 3306,charset = 'utf8')
cursor = conn.cursor()

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'
}

def get_movie_url(url):
    html = requests.get(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,headers = headers)
    selector = etree.HTML(html.text)
    try:
        name = selector.xpath(".//div[@id='content']/h1/span[1]/text()")[0]
        director = selector.xpath(".//div[@id='info']/span[1]/span[@class='attrs']/a/text()")[0]
        actors = selector.xpath(".//div[@id='info']/span[@class='actor']/span[@class='pl']")[0]
        actor = actors.xpath('string(.)')
        style = re.findall('<span property = "v:genre">(.*?)</span>',html.text,re.S)[0]
        country = re.findall('<span class="pl">制片国家/地区:</span>(.*?)<br/>',html.text,re.S)[0]
        release_time = re.findall('上映日期:</span>.*?>(.*?)</span>',html.text,re.S)[0]
        time = re.findall('片长:</span>.*?>(.*?)</span>',html.text,re.S)[0]
        score = selector.xpath(".//strong[@class='ll rating_num']/text()")[0]
        cursor.execute("insert into doubanmovie (name,director,actor,style,country,release_time,time,score) values(%s,%s,%s,%s,%s,%s,%s,%s)",(str(name),str(director),str(actor),str(style),str(country),str(release_time),str(time),str(score)))
        
    except:
        pass

if __name__ == '__main__':
    urls = ['https://movie.douban.com/top250?start={}'.format(str(i)) for i in range (0,250,25)]
    for url in urls:
        get_movie_url(url)
        time.sleep(2)
    conn.commit()

  • 写回答

1条回答 默认 最新

  • homlee ning 2020-11-12 10:27
    关注

    try:

    except:
            pass

    异常打印日志看看,直接pass有错也不知道啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月27日
  • 已采纳回答 8月19日

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算