为了账号安全,请及时绑定邮箱和手机立即绑定

BeautifulSoup的ResultSet,如何遍历全部内容?

BeautifulSoup的ResultSet,如何遍历全部内容?

大话西游666 2019-02-18 00:38:36
目标网页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 href="//www.w3cschool.cn/codecamp/say-hello-to-html-element.html" title="Say Hello to HTML Element"> <i class="icon-codecamp-list icon-codecamp-option"></i> 开始学习HTML标签</a>, <a href="//www.w3cschool.cn/codecamp/headline-with-the-h2-element.html" title="Headline with the h2 Element"> <i class="icon-codecamp-list icon-codecamp-option"></i> HTML 学习h2标签</a>, <a href="//www.w3cschool.cn/codecamp/inform-with-the-paragraph-element.html" title="Inform with the Paragraph Element"> <i class="icon-codecamp-list icon-codecamp-option"></i> HTML 学习p标签</a>, <a href="//www.w3cschool.cn/codecamp/uncomment-html.html" title="Uncomment HTML"> <i class="icon-codecamp-list icon-codecamp-option"></i> 删除HTML的注释</a>] 请问一下这样的格式的数据怎么解析呀目标是把里面的链接和title 保存成csv格式 对应的Tag格式的数据只能找到第一个,使用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') 时为什么只能输出一条呢 结果 {'name': 'Say Hello to HTML Element', 'title': 'www.w3cschool.cn/codecamp/say-hello-to-html-element.html'}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 6858 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信