博爱 Python:为 PyCharm 添加第三方库,例如:BeautifulSoup、Lxml...

我们在用 PyCharm 扒取数据的时候,别忘了几个强大的三方库,可以很方便的格式化你所扒取的数据,让你的数据结构一目了然,从而进一步取到自己想要的数据,下面博爱就给大家介绍下 PyCharm 怎么添加第三方库:

1、打开 PyCharm 的设置,找到 Project ,然后选择第一个,如图:

《博爱 Python:为 PyCharm 添加第三方库,例如:BeautifulSoup、Lxml...》 PyCharm 设置.png

2、找到 BeautifulSoup 最新版后,双击,进入安装界面,如图:

《博爱 Python:为 PyCharm 添加第三方库,例如:BeautifulSoup、Lxml...》 BeautifulSoup 安装.png

3、点击左下角安装后,然后回到设置界面,点击 Apply 按钮,就安装成功了:

《博爱 Python:为 PyCharm 添加第三方库,例如:BeautifulSoup、Lxml...》 2AC60FCA-9B4B-4224-9CB9-2AEDBE1A62B1.png

4、具体导入方法:

# !/usr/bin/python3

'''
-*- coding: utf-8 -*-
@Author  : 博爱1616
@Time    : 2017/10/31 下午4:37
@Software: PyCharm
@File    : demo_BeautifulSoup.py
'''

from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="boaihome"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

# 创建 beautifulsoup 对象
# soup = BeautifulSoup(html, 'lxml')
# 另外,我们还可以用本地 HTML 文件来创建对象,例如
soup2 = BeautifulSoup(open('test2.html'), 'lxml')

'''
上面这句代码便是将本地 test2.html 文件打开,用它来创建 soup 对象

下面我们来打印一下 soup 对象的内容,格式化输出
'''

print('格式化抓取内容:\n%s ' % (soup2.prettify()))

赶紧试试吧!

    原文作者:博爱1616
    原文地址: https://www.jianshu.com/p/7dd10ffd58ea
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞