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

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

BeautifulSoup的ResultSet,如何遍歷全部內(nèi)容?

BeautifulSoup的ResultSet,如何遍歷全部內(nèi)容?

大話西游666 2019-02-18 00:38:36
目標網(wǎng)頁https://www.w3cschool.cn/code... 這個是抓取html def getHtml(url): re = requests.get(url) return re.text index = getHtml(url) index 這個是解析html的方法 def parseHtml(html): soup = BeautifulSoup(index,'html.parser') #soup lessonList= soup.find('div',class_='codecamplist-catalog').find_all('a') return lessonList lessonList = parseHtml(index) lessonList 最后得到的lessonList 是bs4.element.ResultSet 格式 [<a title="Say Hello to HTML Element"> <i class="icon-codecamp-list icon-codecamp-option"></i> 開始學習HTML標簽</a>, <a title="Headline with the h2 Element"> <i class="icon-codecamp-list icon-codecamp-option"></i> HTML 學習h2標簽</a>, <a title="Inform with the Paragraph Element"> <i class="icon-codecamp-list icon-codecamp-option"></i> HTML 學習p標簽</a>, <a title="Uncomment HTML"> <i class="icon-codecamp-list icon-codecamp-option"></i> 刪除HTML的注釋</a>] 請問一下這樣的格式的數(shù)據(jù)怎么解析呀目標是把里面的鏈接和title 保存成csv格式 對應的Tag格式的數(shù)據(jù)只能找到第一個,使用Find_all方法又會報錯。 def getLesson(lessonList): for i in lessonList: lesson={} try: lesson['title'] = i.find('a')['href'].lstrip('//') lesson['name']= i.find('a')['title'] except: print('error') return lesson getLesson(lessonList) # 當上面是 lessonList= soup.find_all('div',class_='codecamplist-catalog') # .find_all('a') 時為什么只能輸出一條呢 結(jié)果 {'name': 'Say Hello to HTML Element', 'title': 'www.w3cschool.cn/codecamp/say-hello-to-html-element.html'}
查看完整描述

1 回答

  • 1 回答
  • 0 關(guān)注
  • 6878 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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