【发布时间】:2021-02-20 11:27:51
【问题描述】:
我有一个 URL,并试图通过 span 标签对上面的数字求和。任何人都可以帮助修改下面的代码来做这样的事情吗?:(网址是:http://py4e-data.dr-chuck.net/comments_1050359.html)
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ')
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
# Look at the parts of a tag
print('TAG:', tag)
print('URL:', tag.get('href', None))
print('Contents:', tag.contents[0])
print('Attrs:', tag.attrs)
【问题讨论】:
标签: python html beautifulsoup count tags