第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按照一個仿例寫了一個爬取amazon網頁的程序,但是有諸多的錯誤,是在不明白,求助!

按照一個仿例寫了一個爬取amazon網頁的程序,但是有諸多的錯誤,是在不明白,求助!

翻翻過去那場雪 2019-05-21 09:28:39
爬取的是Amazon中國,手機->手機通訊->ApplePhone中的商品標題和價格。其URL=https://www.amazon.cn/s/ref=s...我的python代碼如下:importrequestsfrombs4importBeautifulSoupimportre#用于HTML配合查找條件importtime#用于文件名的保存#獲取總頁面數量defget_total_page_number():user_agent='Mozilla/5.0(WindowsNT6.3;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/45.0.2454.101Safari/537.36'headers={'User-Agent':user_agent}#定義頭信息#尋找頁碼的URLurl='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ā)送請求html=res.texthtml_soup=BeautifulSoup(html,"lxml")#建立soup對象,用于處理htmlpage_number_span=html_soup.find('h2',id='s-result-count')#查找id="s-result-count"的h2標簽page_number_code=page_number_span.text#讀取該標簽的文本信息number_list=re.findall(r'(\w*[0-9]+)\w',page_number_code)#使用正則表達式解析出文本中的3個數字total_page_number=(int(number_list[-1])/int(number_list[-2])+1)#計算得出總的頁碼returnint(total_page_number)#返回頁面數字#解析單頁面defparse_single_page(i):url_part1='https://www.amazon.cn/s/ref=sa_menu_digita_l3_siphone?ie=UTF8&page=%d'%i#定義URL動態(tài)前半部分url_part2='&rh=n%3A665002051%2Cp_89%3AApple%2Cn%3A664978051'#定義URL靜態(tài)后半部分url=url_part1+url_part2#拼接完整的URLprint('praseurl:%s'%url)#輸出URL信息user_agent='Mozilla/5.0(WindowsNT6.3;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/45.0.2454.101Safari/537.36'res=requests.get(url,headers=headers)#發(fā)送請求html=res.texthtml_soup=BeautifulSoup(html,"lxml")#建立soup對象,用于處理htmltag_list=html_soup.find_all('li',id=re.compile('^result.*'))#查找id以result開始的li標簽,返回列表#讀取列表中每一個標簽(一個標簽對應一個商品)fortag_infointag_list:#價格解析print(tag_info)price_code=tag_info.find('span',class_="a-size-basea-color-prices-pricea-text-bold")#若價格標簽不空則取出價格文字ifprice_code!=None:#解析商品標題title_code=tag_info.find('h2')#查找標題標簽title=title_code.text#取出標題標簽文字write_data(title,price)#每次解析完成寫入文件#將數據寫入文件defwrite_data(title,price):file_data=time.strftime('%Y-%m-%d',time.localtime(time.time()))#取當前文件日期用于文件命名fn=open('%s.txt'%file_data,'a+')#新建文件對象,以追加模式打開content=title+'\t'+price+'\n'#寫內容,標題和價格以tab分割,末尾增加換行符fn.write(content)#寫入文件fn.close()#解析多頁面并寫入文件defmain():total_page_number=get_total_page_number()#獲得頁面總數foriinrange(1,int(total_page_number)+1):parse_single_page(i)main()報的錯誤如下:AttributeErrorTraceback(mostrecentcalllast)in()51parse_single_page(i)52--->53main()inmain()47#解析多頁面并寫入文件48defmain():--->49total_page_number=get_total_page_number()#獲得頁面總數50foriinrange(1,int(total_page_number)+1):51parse_single_page(i)inget_total_page_number()9html_soup=BeautifulSoup(html,"lxml")#建立soup對象,用于處理html10page_number_span=html_soup.find('h2',id='s-result-count')#查找id="s-result-count"的h2標簽--->11page_number_code=page_number_span.text#讀取該標簽的文本信息12number_list=re.findall(r'(\w*[0-9]+)\w',page_number_code)#使用正則表達式解析出文本中的3個數字13total_page_number=(int(number_list[-1])/int(number_list[-2])+1)#計算得出總的頁碼AttributeError:'NoneType'objecthasnoattribute'text'我解決了一些問題,但是這個問題上網查了還就還是不能解決,請求大神的幫助,謝謝!我基本每行都有注釋,希望能有效幫助大神閱讀,小弟感謝!
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

授人以魚不如授人以漁:
這個答案很簡單啊,首先你要回看這個debug記錄。
從上到下分別是執(zhí)行流程,然后每個執(zhí)行流程所調用的函數以及出錯的相關代碼,具體代碼位置debug給你用--->標記出來了,而我們所真正要看的是最后出錯位置。也就是
--->11page_number_code=page_number_span.text#讀取該標簽的文本信息
這一行;然后結合最后給你的報錯信息:
AttributeError:'NoneType'objecthasnoattribute'text'
此處告訴你的意思是None類型的對象沒有text屬性值。也就是說page_number_span為None,或者說你壓根沒取到page_number_span,然后你訪問None的屬性text自然是沒有的。
                            
查看完整回答
反對 回復 2019-05-21
?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

page_number_span=html_soup.find('h2',id='s-result-count')
看了一下頁面,這個id對應的標簽是span,所以你應該要改成:
page_number_span=html_soup.find('span',id='s-result-count')
                            
查看完整回答
反對 回復 2019-05-21
  • 2 回答
  • 0 關注
  • 336 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號