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 = 'http://py4e-data.dr-chuck.net/comments_745662.html'
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
tags = soup('span')
lst = list()
for tag in tags:
# Look at the parts of a tag
num = int(tag.contents[0])
lst.append(num)
print(sum(lst))
[PY4E] Scraping HTML Data with BeautifulSoup
最新推荐文章于 2024-09-09 23:28:21 发布