Python网络编程与Web服务使用指南
1. 网络编程基础
1.1 使用BeautifulSoup解析HTML
在网络编程中,经常需要从网页中提取数据。可以使用Python的 BeautifulSoup 库来解析HTML文档。以下是一个示例代码:
url = input('Enter - ')
html = urlopen(url).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
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)
这个代码的操作步骤如下:
1. 提示用户输入一个URL。
超级会员免费看
订阅专栏 解锁全文
5万+

被折叠的 条评论
为什么被折叠?



