如何访问以下代码中的第二个跨度?

问题描述 投票:-1回答:3

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

 <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)
python web-scraping beautifulsoup
3个回答
0
投票

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

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)

OUTPUT:

Mar 19, 2019

0
投票

您可以使用next获取日期,请参阅下面的代码!

html = '''
 <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>'''

soup = BeautifulSoup(html,'html.parser')
date = soup.find('span',attrs={'class':'right date'}).next
print(date.strip())

输出:

Mar 19, 2019

0
投票

对于该特定日期,您可以使用更快的单类类选择器

item = soup.select_one('.date').text

如果你想要它们全部

items = [item.text for item in soup.select('.date')]
© www.soinside.com 2019 - 2024. All rights reserved.