python 使用Beautiful Soup从页面中提取数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Beautiful Soup从页面中提取数据相关的知识,希望对你有一定的参考价值。

# To run this, you can install BeautifulSoup
# https://pypi.python.org/pypi/beautifulsoup4

# Or download the file
# http://www.py4e.com/code3/bs4.zip
# and unzip it in the same directory as this file


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_40614.html"
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 = 0
tags = soup('span')
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])
    sum += int(tag.contents[0])
    # print('Attrs:', tag.attrs)
print(sum)

以上是关于python 使用Beautiful Soup从页面中提取数据的主要内容,如果未能解决你的问题,请参考以下文章

python 爬虫学习--Beautiful Soup插件

Python Beautiful Soup 解析库的使用

python 使用Beautiful Soup从页面中提取数据

python 之beautiful soup 4 warning

Python3网络爬虫:使用Beautiful Soup爬取小说

Python3 爬虫Beautiful Soup库的使用