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

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

關(guān)于list的索引?

關(guān)于list的索引?

from?lxml?import?etree import?requests #爬取糗事百科 page?=?eval(input("請輸入需要爬取的總頁數(shù):")) #print(type(page)) #page?=?3 data?=?"" x?=?"" for?p?in?range(1,page+1): ????url?=?"https://www.qiushibaike.com/8hr/page/{}/".format(p) ????print(url) ????headers?=?{ ????????"User-Agent":?"Mozilla/5.0?(Windows?NT?6.3;?Win64;?x64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/64.0.3282.186?Safari/537.36" ????} ????res?=?requests.get(url,?headers?=?headers) ????tree?=?etree.HTML(res.text) ????all?=?tree.xpath('//div[@id="content-left"]/div') ????data?+=?url+'\n' ????for?div?in?all: ????????author?=?div.xpath('.//h2/text()') ????????age?=?div.xpath('.//div[contains(@class,?"articleGender")]/text()') ????????content?=?div.xpath('.//span/text()') ????????xinbie?=?div.xpath('.//div[contains(@class,?"articleGender")]/@class') ????????funny?=?div.xpath('.//span[@class="stats-vote"]/i/text()') ????????conment?=?div.xpath('.//a[@class="qiushi_comments"]/i/text()') ????????if?xinbie?==?'articleGender?manIcon': ????????????x?='男' ????????elif?xinbie?=='articleGender?womenIcon': ????????????x?=?'女' ????????else: ????????????x?=?"性別不明" ????????up?=?'作者:'?+?author[0].strip()?+?'\t性別:'+?x?+'\t年齡:'?+?str(age) ????????middle?=?content[0].strip() ????????bottom?=?'好笑數(shù):'+?funny[0]?+?'\t評論數(shù):'?+?conment[0] ????????data+=?up?+?'\n'?+?middle?+'\n'+bottom+'\n' ????????print('author:',author[0],type(author)) ????????print('age:',?age[0],type(age)) ????????print('content:',?content[0],?type(content)) ????????print('funny:',?funny[0],?type(funny)) ????????print('conment:',?conment[0],?type(conment)) ??????? with?open('xiushibaike_spider.txt',?'w',?encoding="utf-8")?as?f: ????f.write(data)爬取了5個字段,類型都為list,為什么唯獨age[0]提示? ?IndexError: list index out of range? ?? ?2. xinbie的判斷那里,我知道是list和字符串不能比較,怎么改才對?
查看完整描述

1 回答

已采納
?
產(chǎn)品經(jīng)理不是經(jīng)理

TA貢獻(xiàn)481條經(jīng)驗 獲得超143個贊

1、部分age沒有數(shù)據(jù),爬取到的為空,列表里面為空。

2、把list的數(shù)據(jù)轉(zhuǎn)為字符串即可比較。''.join(list)即可將列表轉(zhuǎn)為字符串。

查看完整回答
反對 回復(fù) 2018-03-26
  • 1 回答
  • 0 關(guān)注
  • 1747 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號