鍍金池/ 問答/數(shù)據(jù)分析&挖掘  Python/ 按照一個(gè)仿例寫了一個(gè)爬取amazon網(wǎng)頁(yè)的程序,但是有諸多的錯(cuò)誤,是在不明白,求

按照一個(gè)仿例寫了一個(gè)爬取amazon網(wǎng)頁(yè)的程序,但是有諸多的錯(cuò)誤,是在不明白,求助!

爬取的是Amazon中國(guó),手機(jī)->手機(jī)通訊->Apple Phone中的商品標(biāo)題和價(jià)格。
其URL=https://www.amazon.cn/s/ref=s...
我的python代碼如下:

import requests
from bs4 import BeautifulSoup
import re #用于HTML配合查找條件
import time #用于文件名的保存

#獲取總頁(yè)面數(shù)量
def get_total_page_number():
    user_agent = 'Mozilla/5.0 (Windows NT 6.3;WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/45.0.2454.101 Safari/537.36'
    headers = {'User-Agent':user_agent} #定義頭信息
    # 尋找頁(yè)碼的URL
    url = 'https://www.amazon.cn/s/ref=sa_menu_digita_l3_siphone?ie=UTF8&page=1&rh=n%3A665002051%2Cp_89%3AApple%2Cn%3A664978051'
    res = requests.get(url,headers=headers) #發(fā)送請(qǐng)求
    html = res.text
    html_soup = BeautifulSoup(html,"lxml") #建立soup對(duì)象,用于處理html
    page_number_span = html_soup.find('h2',id='s-result-count') #查找id="s-result-count"的h2標(biāo)簽
    page_number_code = page_number_span.text #讀取該標(biāo)簽的文本信息
    number_list = re.findall(r'(\w*[0-9]+)\w',page_number_code) #使用正則表達(dá)式解析出文本中的3個(gè)數(shù)字
    total_page_number = (int(number_list[-1])/int(number_list[-2])+1) #計(jì)算得出總的頁(yè)碼
    return int(total_page_number) #返回頁(yè)面數(shù)字

#解析單頁(yè)面
def parse_single_page(i):
    url_part1 = 'https://www.amazon.cn/s/ref=sa_menu_digita_l3_siphone?ie=UTF8&page=%d' % i #定義URL動(dòng)態(tài)前半部分
    url_part2 = '&rh=n%3A665002051%2Cp_89%3AApple%2Cn%3A664978051' #定義URL靜態(tài)后半部分
    url = url_part1 + url_part2 #拼接完整的URL
    print ('prase url: %s' % url) #輸出URL信息
    user_agent = 'Mozilla/5.0 (Windows NT 6.3;WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/45.0.2454.101 Safari/537.36'
    res = requests.get(url,headers=headers) #發(fā)送請(qǐng)求
    html = res.text
    html_soup = BeautifulSoup(html,"lxml") #建立soup對(duì)象,用于處理html
    tag_list = html_soup.find_all('li', id=re.compile('^result.*')) #查找id以result開始的li標(biāo)簽,返回列表
    #讀取列表中每一個(gè)標(biāo)簽(一個(gè)標(biāo)簽對(duì)應(yīng)一個(gè)商品)
    for tag_info in tag_list:
        #價(jià)格解析
        print (tag_info)
        price_code = tag_info.find('span', class_="a-size-base a-color-price s-price a-text-bold")
        #若價(jià)格標(biāo)簽不空則取出價(jià)格文字
        if price_code != None:
            #解析商品標(biāo)題
            title_code = tag_info.find('h2') #查找標(biāo)題標(biāo)簽
            title = title_code.text #取出標(biāo)題標(biāo)簽文字
            write_data(title,price) #每次解析完成寫入文件
            
#將數(shù)據(jù)寫入文件
def write_data(title,price):
    file_data = time.strftime('%Y-%m-%d',time.localtime(time.time())) #取當(dāng)前文件日期用于文件命名
    fn = open('%s.txt' % file_data,'a+') #新建文件對(duì)象,以追加模式打開
    content = title + '\t' + price + '\n' #寫內(nèi)容,標(biāo)題和價(jià)格以tab分割,末尾增加換行符
    fn.write(content) #寫入文件
    fn.close()
    
#解析多頁(yè)面并寫入文件
def main():
    total_page_number = get_total_page_number() #獲得頁(yè)面總數(shù)
    for i in range(1,int(total_page_number)+1):
        parse_single_page(i)

main()

報(bào)的錯(cuò)誤如下:

AttributeError                            Traceback (most recent call last)
<ipython-input-5-5527ff76ca42> in <module>()
     51         parse_single_page(i)
     52 
---> 53 main()

<ipython-input-5-5527ff76ca42> in main()
     47 #解析多頁(yè)面并寫入文件
     48 def main():
---> 49     total_page_number = get_total_page_number() #獲得頁(yè)面總數(shù)
     50     for i in range(1,int(total_page_number)+1):
     51         parse_single_page(i)

<ipython-input-5-5527ff76ca42> in get_total_page_number()
      9     html_soup = BeautifulSoup(html,"lxml") #建立soup對(duì)象,用于處理html
     10     page_number_span = html_soup.find('h2',id='s-result-count') #查找id="s-result-count"的h2標(biāo)簽
---> 11     page_number_code = page_number_span.text #讀取該標(biāo)簽的文本信息
     12     number_list = re.findall(r'(\w*[0-9]+)\w',page_number_code) #使用正則表達(dá)式解析出文本中的3個(gè)數(shù)字
     13     total_page_number = (int(number_list[-1])/int(number_list[-2])+1) #計(jì)算得出總的頁(yè)碼

AttributeError: 'NoneType' object has no attribute 'text'

我解決了一些問題,但是這個(gè)問題上網(wǎng)查了還就還是不能解決,請(qǐng)求大神的幫助,謝謝!
我基本每行都有注釋,希望能有效幫助大神閱讀,小弟感謝!

回答
編輯回答
護(hù)她命

授人以魚不如授人以漁:

這個(gè)答案很簡(jiǎn)單啊,首先你要回看這個(gè)debug記錄。

從上到下分別是執(zhí)行流程,然后每個(gè)執(zhí)行流程所調(diào)用的函數(shù)以及出錯(cuò)的相關(guān)代碼,具體代碼位置debug給你用--->標(biāo)記出來了,而我們所真正要看的是最后出錯(cuò)位置。
也就是

---> 11     page_number_code = page_number_span.text #讀取該標(biāo)簽的文本信息

這一行;
然后結(jié)合最后給你的報(bào)錯(cuò)信息:

AttributeError: 'NoneType' object has no attribute 'text'

此處告訴你的意思是None類型的對(duì)象沒有text屬性值。
也就是說page_number_spanNone,或者說你壓根沒取到page_number_span,然后你訪問None的屬性text自然是沒有的。

2017年6月28日 07:30
編輯回答
有點(diǎn)壞
page_number_span = html_soup.find('h2',id='s-result-count')

看了一下頁(yè)面,這個(gè)id對(duì)應(yīng)的標(biāo)簽是span,所以你應(yīng)該要改成:

page_number_span = html_soup.find('span',id='s-result-count')
2017年4月11日 03:18