Python安装三方模块
一、Python修改镜像源
相当于是从网上下载插件,python的三方模块全部储存在模块仓库
python默认的官方仓库服务器在国外。那么,如何快速的从国外服务器安装模块?
修改镜像源,国内有很多像豆瓣、阿里云、清华云等与国外服务器实时同步的服务器。因此,我们可以将镜像源修改为国内服务器镜像源
windows:
二、Python安装三方模块
方法一:使用命令安装
windows系统:pip install 模块名
mac、linux系统:pip3 install 模块名
方法二:使用pycharm自带的可视化界面安装
setting --> project:项目名 --> python interpreter --> + --> 搜索模块名 --> install package
Python三方模块的使用
一、requests模块
作用:requests从本机(本地网络IP地址)向网站服务器发送请求。目标网站服务器给本机返回响应结果。
import requests
URL = 'https://www.baidu.com'
Headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
response = requests. get( url= URL, headers= Headers)
print ( response)
print ( response. status_code)
response. encoding = 'utf-8'
print ( response. text)
有了字符串类型的源代码,便可以提取网页中我们所需要的信息。
方式一:使用正则表达式匹配。
方式二:使用BeautifulSoup4模块解析数据。
方式三:使用XPath路径选择器解析路径
二、BeautifulSoup4模块
BeautifulSoup是一个用于从HTML文件中提取数据的模块。
使用BeautifulSoup模块,你就可以从HTML中提取到你想要的任何数据。
BeautifulSoup4是BeautifulSoup系列模块的第四代。
BeautifulSoup4模块的安装:
Windows:pip install BeautifulSoup4
Mac、Linux:pip3 install BeautifulSoup4
html_str = """
<html>
<head><title>The Dormouse's story</title></head>
<body>
<p class="title">
<b>The Dormouse's story</b>
</p>
<p class="story" id="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<p>Test</p></a>;
and they lived at the bottom of a well.
</p>
<p class="story" id="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup( html_str, 'html.parser' )
print ( soup)
什么是树结构?
使用树结构的原因?html网页结构就是树结构,便于更快更方便的查找信息。
从网页中提取方法和属性
1.select:使用CSS选择器(标签选择器、id选择器、class选择器、父子选择器、后代选择器、nth-child选择器等)从树结构中遍历符合的结果,存放在列表中。
2.select_one:使用CSS选择器(标签选择器、id选择器、class选择器、父子选择器、后代选择器、nth-child选择器等)从树结构中拿到符合条件的第一结果,这个结果依旧是树结构。
3.text:从树结构中得到标签中的内容,结果为字符串。
4.attrs:从树结构的标签中获得属性名对应的属性值,结果为字符串。
Q1:从soup这个树结构中提取所有的p标签
标签选择器:只写标签名,能从树结构中拿到所有的目标标签
p_list = soup. select( 'p' )
print ( p_list)
Q2:提取body标签下的所有p子标签
父子选择器:>右边的是子标签,左边是父标签
body > p从整个树结构soup中先找到body标签,再去找body标签的p子标签
p_list1 = soup. select( 'body > p' )
print ( p_list1)
Q3:提取body标签下的所有p标签
后代选择器:祖先标签和后代标签使用空格隔开,祖先在左,后代在右。
注意:父子关系一定是后代关系,后代关系不一定是父子关系。
p_list2 = soup. select( 'body p' )
print ( p_list2)
Q4:提取body标签下的第一个p子标签
class选择器,class属性对应的属性值使用.调用
p = soup. select( 'body > p.title' )
print ( p)
Q5:提取body标签的后两个p子标签
id选择器:id属性名对应的属性值使用#调用
p_list3 = soup. select( 'body > p#story' )
print ( p_list3)
Q6:提取body标签下的第二个p子标签的第三个a标签的标签内容和href属性值
nth-child选择器:不区分标签名,获取同级所有标签的第n个
text_str = soup. select( 'body > p:nth-child(2) > a#link3' ) [ 0 ] . text
print ( text_str)
text_str2 = soup. select_one( 'body > p:nth-child(2) > a#link3' ) . text
print ( text_str2)
href_str = soup. select( 'body > p:nth-child(2) > a#link3' ) [ 0 ] . attrs[ 'href' ]
print ( href_str)
CSS选择器作用:按照树结构,寻找所需要的内容。(限定条件),所有的CSS选择器能够随意组合使用。
三、tqdm模块(进度条模块)
1.安装
windows:pip install tqdm
mac、linux系统:pip3 install tqdm
2.使用
有进度条没有print,有print没有进度条