在 Python 中使用 BeautifulSoup 解析数据 [英] Parsing out data using BeautifulSoup in Python

查看:13
本文介绍了在 Python 中使用 BeautifulSoup 解析数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 BeautifulSoup 解析 DOM 树并提取作者姓名.下面是一段 HTML 代码,用于显示我将要抓取的代码的结构.

<身体><div class="list-authors"><span class="descriptor">作者:</span><a href="/find/astro-ph/1/au:+Lin_D/0/1/0/all/0/1">林大成</a>,<a href="/find/astro-ph/1/au:+Remillard_R/0/1/0/all/0/1">Ronald A. Remillard</a>,<a href="/find/astro-ph/1/au:+Homan_J/0/1/0/all/0/1">Jeroen Homan</a>

<div class="list-authors"><span class="descriptor">作者:</span><a href="/find/astro-ph/1/au:+Kosovichev_A/0/1/0/all/0/1">A.G.科索维奇夫

<!--还有许多其他具有这种结构的 div 标签-->

我的困惑在于,当我执行soup.find 时,它会找到我正在搜索的第一个div 标记.之后,我搜索所有a"链接标签.在这个阶段,我如何从每个链接标签中提取作者姓名并打印出来?有没有办法使用 BeautifulSoup 或者我需要使用 Regex?如何继续遍历其他所有 div 标签并提取作者姓名?

导入重新导入 urllib2,sys从 BeautifulSoup 导入 BeautifulSoup, NavigableStringhtml = urllib2.urlopen(地址).read()汤 = BeautifulSoup(html)尝试:authordiv = 汤.find('div', attrs={'class': 'list-authors'})链接= tds.findAll('a')对于链接中的链接:打印 ''.join(link[0].contents)#迭代整个页面和印刷作者除了 IOError:打印IO 错误"

解决方案

只需将 findAll 用于您为链接所做的 div 链接

对于soup.findAll('div', attrs={'class': 'list-authors'})中的authordiv:

I am attempting to use BeautifulSoup to parse through a DOM tree and extract the names of authors. Below is a snippet of HTML to show the structure of the code I'm going to scrape.

<html>
<body>
<div class="list-authors">
<span class="descriptor">Authors:</span> 
<a href="/find/astro-ph/1/au:+Lin_D/0/1/0/all/0/1">Dacheng Lin</a>, 
<a href="/find/astro-ph/1/au:+Remillard_R/0/1/0/all/0/1">Ronald A. Remillard</a>, 
<a href="/find/astro-ph/1/au:+Homan_J/0/1/0/all/0/1">Jeroen Homan</a> 
</div>
<div class="list-authors">
<span class="descriptor">Authors:</span> 
<a href="/find/astro-ph/1/au:+Kosovichev_A/0/1/0/all/0/1">A.G. Kosovichev</a>
</div>

<!--There are many other div tags with this structure-->
</body>
</html>

My point of confusion is that when I do soup.find, it finds the first occurrence of the div tag that I'm searching for. After that, I search for all 'a' link tags. At this stage, how do I extract the authors names from each of the link tags and print them out? Is there a way to do it using BeautifulSoup or do I need to use Regex? How do I continue iterating over every other other div tag and extract the authors names?

import re
import urllib2,sys
from BeautifulSoup import BeautifulSoup, NavigableString
html = urllib2.urlopen(address).read()
    soup = BeautifulSoup(html)

    try:

        authordiv = soup.find('div', attrs={'class': 'list-authors'})
        links=tds.findAll('a')


        for link in links:
            print ''.join(link[0].contents)

        #Iterate through entire page and print authors


    except IOError: 
        print 'IO error'

解决方案

just use findAll for the divs link you do for the links

for authordiv in soup.findAll('div', attrs={'class': 'list-authors'}):

这篇关于在 Python 中使用 BeautifulSoup 解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆