【问题标题】:How to extract time class from HTML in python?如何从python中的HTML中提取时间类?
【发布时间】:2021-06-03 16:15:29
【问题描述】:

我通过beautifulsoup在python中有一段HTML代码,但无法从中检索到所需的时间标签。

HTML is called K:

<time class="dtstart" datetime="05 December 201710:30 AM GMT" id="x-event-date" xcdate="1512469800950">
<a class="action pull-right print-cat" data-href="/en/aus/2017/some-url-data-l17407.html" data-modalid="catalogueModal" data-toggle="modal" href="/en/auctions/ecatalogue/lot.print.L17407.html" style="display: none;">Print My Catalogue (0)</a>
<ul class="breadcrumb inline">
<li>
<a href="/en/aus/2017/some-url-data-l17407.html"><span class="active">Smartphone and watches</span></a>
</li>
</ul>
</time>    

我可以提取除时间以外的所有标签:

K.a :
<a class="action pull-right print-cat" data-href="/en/aus/2017/some-url-data-l17407.html" data-modalid="catalogueModal" data-toggle="modal" href="/en/auctions/ecatalogue/lot.print.L17407.html" style="display: none;">Print My Catalogue (0)</a>

K.li:
<li>
<a href="/en/aus/2017/some-url-data-l17407.html"><span class="active">Smartphone and watches</span></a>
</li>

K.time:
Nothing prints

我也尝试了以下解决方案:

K.find('time', {'class':'dtstart'})
Nothing prints

K.find('a', {'class':'action pull-right print-cat'})
<a class="action pull-right print-cat" data-href="/en/aus/2017/some-url-data-l17407.html" data-modalid="catalogueModal" data-toggle="modal" href="/en/auctions/ecatalogue/lot.print.L17407.html" style="display: none;">Print My Catalogue (0)</a>

当我们检查 K 时,我们会看到以下内容:

Signature:      K(*args, **kwargs)
Type:           Tag
String form:   
<time class="dtstart" datetime="05 December 201710:30 AM GMT" id="x-event-date" xcdate="1512469800950">
<a class="action pull-right print-cat" data-href="/en/aus/2017/some-url-data-l17407.html" data-modalid="catalogueModal" data-toggle="modal" href="/en/auctions/ecatalogue/lot.print.L17407.html" style="display: none;">Print My Catalogue (0)</a>
<ul class="breadcrumb inline">
<li>
<a href="/en/aus/2017/some-url-data-l17407.html"><span class="active">Smartphone and watches</span></a>
</li>
</ul>
</time>  
Length:         5
File:           ~/.local/lib/python3.6/site-packages/bs4/element.py
Source:    

时间标签怎么可能没有被提取?

【问题讨论】:

  • 源网址和预期输出值是多少?您是否确认它不是动态加载的?似乎该网页正在为此制作额外的 xhr,或者正在使用 js 从其他地方计算/填充

标签: python html beautifulsoup tags


【解决方案1】:

我仍然不确定为什么我一开始就无法收到它,但 Chris Doyle 为成功铺平了道路。我们可以简单地重新调整它并获得所需的结果:

Date=soup(str(K), "html.parser").time.attrs["datetime"]
print(Date)

#Output
{'class': ['dtstart'], 'datetime': '05 December 201710:30 AM GMT', 'id': 'x-event-date', 'xcdate': '1512469800950'}

【讨论】:

    【解决方案2】:

    您需要仔细检查您在脚本中收到的 html 代码。在您的问题中使用带有 html 的最小示例,很明显 bs4 可以获得时间标签。

    from bs4 import BeautifulSoup
    
    html_string = """<time class="dtstart" datetime="05 December 201710:30 AM GMT" id="x-event-date" xcdate="1512469800950">
    <a class="action pull-right print-cat" data-href="/en/aus/2017/some-url-data-l17407.html" data-modalid="catalogueModal" data-toggle="modal" href="/en/auctions/ecatalogue/lot.print.L17407.html" style="display: none;">Print My Catalogue (0)</a>
    <ul class="breadcrumb inline">
    <li>
    <a href="/en/aus/2017/some-url-data-l17407.html"><span class="active">Smartphone and watches</span></a>
    </li>
    </ul>
    </time>"""
    
    k = BeautifulSoup(html_string, features="lxml")
    print(k.time.attrs)
    

    输出

    {'class': ['dtstart'], 'datetime': '05 December 201710:30 AM GMT', 'id': 'x-event-date', 'xcdate': '1512469800950'}
    

    【讨论】:

      猜你喜欢
      • 2023-01-09
      • 1970-01-01
      • 2022-01-04
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      相关资源
      最近更新 更多