0
输入URL http://py4e-data.dr-chuck.net/comments_42.html(Python 3的操作系统Win7的)列表不显示预期的输出
当我运行这段代码,预期的输出是包含数字,是标签这是内部列表在程序中被解析。但我所得到的是列表中的最后一个数字。
请更正程序,以显示在所有标签目前号码的列表被解析
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl
import re
# 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()
# html.parser is the HTML parser included in the standard Python 3 library.
# information on other HTML parsers is here:
# http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser
soup = BeautifulSoup(html, "html.parser")
# Retrieve all of the anchor tags
sum_of_num = 0
tags = soup('tr')
for tag in tags:
# Look at the parts of a tag
print('TAG:', tag)
num = re.findall('[0-9]+',str(tag))
print(num)
你可以选择span.comments,即:'soup.select('span.comments')'。然后获取文本,转向int和sum。 –