在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ 很奇怪,為什么我的爬蟲程序在爬到第100部電影,貓鼠游戲的時候爬不下來?

很奇怪,為什么我的爬蟲程序在爬到第100部電影,貓鼠游戲的時候爬不下來?

現(xiàn)在的代碼

import requests
import re

path = 'F:豆瓣Top250.txt'
#抓取網(wǎng)頁
def getHTMLText(url):
    try:
        r = requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""

#分析網(wǎng)頁,提取所需信息
def parseHTML(info,html):
    try:
        tlt = re.findall(r'\"title\"\>[\w\u4e00-\u9fa5].*[\w \u4e00-\u9fa5]*',html)
        dirlt = re.findall(r'導演\:\s[\w \u4e00-\u9fa5]+[\·\/\s]*[\w \u4e00-\u9fa5]*',html)
        yearlt = re.findall(r'[\d]{4}\&nbsp',html)
        coult = re.findall(r'\&nbsp\;[\s\u4e00-\u9fa5]+\&nbsp',html)
        comlt = re.findall(r'inq\"\>.+\<',html)
        rlt = re.findall(r'\"v:average\"\>[0-9]\.[0-9]',html)
        
        for i in range(len(tlt)):      #電影個數(shù)
            title = re.split('>|<',tlt[i])[1]   #用>隔開
            direct = dirlt[i].split(': ')[1]
            year = yearlt[i].split('&')[0]
            country = re.split(';|&',coult[i])[2]
            comment = re.split('>|<',comlt[i])[1]
            rank = rlt[i].split('>')[1]   #用>隔開
            info.append([title,year,direct,country,comment,rank])
    except:
        print("")

def printInfo(info):
    tplt = "{:\u3000>7}:{:<7}"
    count = 0
    for g in info:
        with open(path,'a',encoding='utf-8') as f:
            count = count + 1
            print(tplt.format('序號',count,chr(12288)))
            print(tplt.format('電影名稱',g[0],chr(12288)))
            print(tplt.format('年份',g[1],chr(12288)))
            print(tplt.format('導演',g[2],chr(12288)))
            print(tplt.format('國家',g[3],chr(12288)))
            print(tplt.format('簡短點評',g[4],chr(12288)))
            print(tplt.format('豆瓣評分',g[5],chr(12288)))
            print("-------------------------------------")
            f.write(tplt.format('序號',count,chr(12288))+'\n')
            f.write(tplt.format('電影名稱',g[0],chr(12288))+'\n')
            f.write(tplt.format('年份',g[1],chr(12288))+'\n')
            f.write(tplt.format('導演',g[2],chr(12288))+'\n')
            f.write(tplt.format('國家',g[3],chr(12288))+'\n')
            f.write(tplt.format('簡短點評',g[4],chr(12288))+'\n')
            f.write(tplt.format('豆瓣評分',g[5],chr(12288))+'\n')
            f.write("-------------------------------------"+'\n')
                
def main():
    start_url = "https://movie.douban.com/top250?start="
    depth = 10   #總共10頁
    infomation = [] #用來存儲相關信息
    
    for i in range(depth):
        try:
            url = start_url+str(25*i)
            html = getHTMLText(url)
            parseHTML(infomation,html)
        except:
                print("")
    printInfo(infomation)
main()

而豆瓣那也所在的網(wǎng)頁代碼如下:

  <li>
            <div class="item">
                <div class="pic">
                    <em class="">100</em>
                    <a >
                        <img width="100" alt="貓鼠游戲" src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p453924541.webp" class="">
                    </a>
                </div>
                <div class="info">
                    <div class="hd">
                        <a  class="">
                            <span class="title">貓鼠游戲</span>
                                    <span class="title">&nbsp;/&nbsp;Catch Me If You Can</span>
                                <span class="other">&nbsp;/&nbsp;逍遙法外  /  神鬼交鋒(臺)</span>
                        </a>


                            <span class="playable">[可播放]</span>
                    </div>
                    <div class="bd">
                        <p class="">
                            導演: 史蒂文·斯皮爾伯格 Steven Spielberg&nbsp;&nbsp;&nbsp;主演: 萊昂納多·迪卡普里奧 L...<br>
                            2002&nbsp;/&nbsp;美國 加拿大&nbsp;/&nbsp;傳記 犯罪 劇情
                        </p>

                        

我用正則表達式匹配工具,是可以匹配出貓鼠游戲的,但是為何這里不行,求好心人幫忙,學爬蟲,總是遇到各種迷之問題o(╯□╰)o

回答
編輯回答
有點壞

我反對第一個回答,根本都沒有仔細分析程序和頁面就胡扯。

樓主你注意一下大鬧天宮的上映時間就知道了,它的格式跟其他電影的格式不一樣。
這一步匹配錯了,導致了后面的一連串問題。
其中還包括一個問題就是,從大鬧天宮開始到第99個電影,程序里打印的上映時間都與頁面顯示的不一樣。

這個頁面的html很整齊,用正則不如用xpath,正則要考慮的問題太多。
百度看下xpath怎么用吧。

2017年1月2日 21:02
編輯回答
賤人曾

被服務器判斷為非人類了,爬蟲時候也要考慮一下人家服務器的感受吧,你可以讓程序爬取了一定數(shù)量后暫停一會
一些基本的反爬策略

2017年8月10日 04:28