【问题标题】:Extract data from <span> section of web page从网页的 <span> 部分提取数据
【发布时间】:2018-11-04 05:15:25
【问题描述】:

我正在尝试使用 Python 和 Beautiful soup 打开一个链接并提取嵌入在标签中的数据。我试过这样做,但我的知识已经耗尽。

这是我的代码部分以及我试图从中获取数据的文本的样子

sauce = urllib.request.urlopen(link).read() #link is the url
soup = BeautifulSoup(sauce,'lxml')

yy = soup.select('span[id^=ctl00_ContentPlaceHolder1_Label1]')
y = yy[0]
print(y)

print(y) 产生以下数据:

        '<span id="ctl00_ContentPlaceHolder1_Label1"><div style="width:100%;clear:both;overflow:hidden;">\
        <div style="width:17%;float:left;margin-right:10px;"><span style="font-size:16px;font-weight:bold;"> \
        Licensee:</span></div><div style="float:left;"><span style="font-size:14px;font-weight:bold;">Company, INC.</span></div></div><div \
        style="width:100%;clear:both;overflow: hidden;"><div style="width:17%;float:left;margin-right:10px;"> \
        <span style="font-size:16px;font-weight:bold;">Facility:</span></div><div style="float:left;"> \
        <span style="font-size:14px;font-weight:bold;">Joes Shop</span></div></div><br/><b>Status:</b> \
        Licensed<br/><b>JOE SMITH - Director</b><br/><b>Phone:</b> (555)555-5555<br/> <span style="font-size:8pt"><table \
        border="1" style="padding:1px 1px 5px 1px;border:1px solid #999999;width:497px;border-collapse:collapse;"><tr><td \
        width="50%"><b>Daytime Hours:</b>  07:30 AM - 03:30 PM</td><td width="50%"><b>Nighttime Hours:</b>   \
        N/A - N/A</td></tr><tr><td width="50%"><b>Daytime Ages:</b>  4 YRS Through 5 YRS</td><td width="50%"><b> \
        Nighttime Ages:</b>  N/A</td></tr></table></span><br/><span style="font-size:12px;font-weight:bold;"> \
        Mailing Address:</span><br/><span style="font-size:12px;">1909 CENTRAL PARK</span><br/> \
        <span style="font-size:12px;">NEW YORK</span>, <span style="font-size:12px;">NY</span> \
        <span style="font-size:12px;">58756</span><br/><br/><span style="font-size:12px;font-weight:bold;"> \
        Street Address:</span><br/><span style="font-size:12px;">3996 Rhode Ave</span><br/> \
        <span style="font-size:12px;">Cleveland</span>, <span style="font-size:12px;">OH</span> <span style="font-size:12px;">58475</span></span>'

我试过了:

ystring = y.getText(separator=u' ')

但这只给我留下了所有的文字和标题,我想要的只是真实姓名、电话号码、地址等。

具体来说,我试图从中提取以下内容: 被许可人(Company, Inc)、设施(Joes Shop)、状态(许可)、主管 (Joe Smith)、电话 ((555) 555-5555)、白天时间 (07:30 AM - 03:30 PM)、夜间时间(N/A - N/A), 日间年龄 (4 岁至 5 岁), 夜间年龄 (N/A), 邮寄地址 (1909 Central Park, New York, NY, 58756 (separate Street, City, State, zip)用逗号和街道地址(3996 Rhode Ave, Cleveland, OH 58475))

非常感谢任何想法或建议。

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    .descendants 为您提供标签的所有子代,包括子代的子代。您可以使用它来搜索所有 NavigableString 类型(并删除空的类型)。下面的 sn-p 就可以做到这一点。

    从那里开始,这取决于您要做什么:也许使用正则表达式来搜索列表并根据您的规范格式化部分,如果您解析的页面看起来都一样并且列表的索引将是,则执行一些静态提取相同或尝试一些机器学习来解析内容。

    sauce = urllib.request.urlopen(link).read() #link is the url
    soup = BeautifulSoup(sauce,'lxml')
    span = soup('span', attrs={'id': 'ctl00_ContentPlaceHolder1_Label1'})
    
    [c.strip() for c in soup.span.descendants if type(c) == NavigableString and len(c.strip()) > 0]
    

    【讨论】:

      【解决方案2】:

      我认为你可以从 y 中提取数据,然后重新组合它们。

      import re
      html = "..."
      print([ele.strip() for ele in re.findall("(?<=>).*?(?=<)",html) if ele.strip() not in ["",","]])
      

      输出

      ['Licensee:', 'Company, INC.', 'Facility:', 'Joes Shop', 'Status:', 'Licensed',
       'JOE SMITH - Director', 'Phone:', '(555)555-5555',
       'Daytime Hours:', '07:30 AM - 03:30 PM',
       'Nighttime Hours:', 'N/A - N/A', 'Daytime Ages:', '4 YRS Through 5 YRS',
       'Nighttime Ages:', 'N/A', 'Mailing Address:', '1909 CENTRAL PARK',
       'NEW YORK', 'NY', '58756', 'Street Address:', '3996 Rhode Ave', 'Cleveland', 'OH', '58475']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-17
        • 2011-08-08
        • 1970-01-01
        • 2012-07-13
        • 1970-01-01
        • 2011-12-21
        • 1970-01-01
        相关资源
        最近更新 更多