爬取的是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)求大神的幫助,謝謝!
我基本每行都有注釋,希望能有效幫助大神閱讀,小弟感謝!
授人以魚不如授人以漁:
這個(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_span
為None
,或者說你壓根沒取到page_number_span
,然后你訪問None的屬性text
自然是沒有的。
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問,美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。