如何在下面的代码中访问第二个跨度? [英] How to access the second span in the below code?

查看:58
本文介绍了如何在下面的代码中访问第二个跨度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问一个带日期的跨度,但是当我写article.h3.span时,它给出了第一个跨度(/).如何访问带有日期的跨度?

I want to access a span with the date in but when I write article.h3.span, it gives the first span (/). How can I access a span with the date?

 <a class="category-link" href="https://www.japantimes.co.jp/news_category/world/">
  World
 </a>
 <span>
  /
 </span>
 <a class="category-link" href="https://www.japantimes.co.jp/news_category/crime-legal-world/">
  Crime &amp; Legal
 </a>
 <span class="right date">
  Mar 19, 2019
 </span>
</h3>

下面是代码:

from bs4 import BeautifulSoup
ssl._create_default_https_context = ssl._create_unverified_context
article = "https://www.japantimes.co.jp/tag/cybersecurity/page/1/"
page = urllib.request.urlopen(article)
soup = BeautifulSoup(page, 'html.parser')
article = soup.find('article')
date = article.h3.span.text
print(date)

推荐答案

使用span标记中的class=right date可以做到:

Using the class=right date from the span tag would do it:

from bs4 import BeautifulSoup

article = "https://www.japantimes.co.jp/tag/cybersecurity/page/1/"
page = urllib.request.urlopen(article)
soup = BeautifulSoup(page, 'html.parser')
date = soup.find('span', class_ ="right date")
print(date.text)

输出:

Mar 19, 2019

这篇关于如何在下面的代码中访问第二个跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆