这里写目录标题
- 一.昨日回顾
- 二.今日内容
- 1.bs4遍历文档树
- 2.bs4的搜索文档树
- 3.find_all的其他参数
- 4.css选择器
- 5.selenium的介绍
- 6.selenium的使用
- 7.模拟登陆百度
- 8.selenium的其他使用
一.昨日回顾
1 项目演示
-mkvirtualenv -p python3 gist
-git clone xxx
-pycharm打开
-确保python解释器3.6(如果是3.6以上,百度改源码)
-修改配置文件连接数据库使用sqlite
-创建一个超级用户,把别人密码全改成自己
-查看项目流程及代码
2 bs4模块,html解析,(解析json,使用json模块)
-在浏览器中看到的数据,会比使用requests模块拿回来的数据多(requests模块不能主动发送ajax请求,渲染页面---》request-html)
3 bs4 使用
-遍历文档树
-soup.body.p
-取属性 soup.body.p.attrs 或者 soup.body.p['name']
-取文本soup.body.p.text 把子子孙孙的文本拼到一起
-取文本soup.body.p.string 只取当前p标签的文本,如果还有子标签,取出None
-取文本soup.body.p.strings 取出子子孙孙标签的文本,放到一个生成器中
二.今日内容
1.bs4遍历文档树
'''
#遍历文档树:即直接通过标签名字选择,特点是选择速度快,但如果存在多个相同的标签则只返回第一个
#1、用法
#2、获取标签的名称
#3、获取标签的属性
#4、获取标签的内容
#5、嵌套选择
#6、子节点、子孙节点
#7、父节点、祖先节点
#8、兄弟节点
'''
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" id='id_pp' name='lqz'>asdfasdf<b>asdfas</b><span>span<b>bbb</b></span></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>
"""
soup=BeautifulSoup(html_doc,'lxml')
2.bs4的搜索文档树
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" id='id_pp' name='lqz'>asdfasdf<b>asdfas</b><span>span<b>bbb</b></span></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="sister1" 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>
"""
soup=BeautifulSoup(html_doc,'lxml')
3.find_all的其他参数
res=soup.body.find_all(name=True,limit=1)
soup.find()
print(res)
4.css选择器
'''
#id_p :#id号
.class1 :.类名
body :标签名
p>b :p的亲儿子b
p b :p的的后代b
'''
5.selenium的介绍
1 自动化测试工具,控制浏览器,模拟人的行为,做爬虫为了解决使用requests模块无法执行ajax获取数据
2 使用selenium+半人工登录,获取cookie-----》给requests模块使用
6.selenium的使用
1 安装模块 pip3 install selenium
2 驱动浏览器(需要浏览器驱动---》不同去不同浏览器官网下载),下载的驱动要跟浏览器版本对应
http://npm.taobao.org/mirrors/chromedriver/
3 chrom浏览器为例
from selenium import webdriver
7.模拟登陆百度
from selenium import webdriver
import time
bro=webdriver.Chrome(executable_path='chromedriver.exe')
bro.get('http://www.baidu.com')
bro.implicitly_wait(100)
login_a=bro.find_element_by_link_text('登录')
login_a.click()
username_login_p=bro.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn')
username_login_p.click()
username_input=bro.find_element_by_name('userName')
username_input.send_keys('306334678@qq.com')
password_input=bro.find_element_by_id('TANGRAM__PSP_11__password')
password_input.send_keys('sss')
login_button=bro.find_element_by_id('TANGRAM__PSP_11__submit')
time.sleep(5)
login_button.click()
time.sleep(10)
print(bro.get_cookies())
bro.close()
8.selenium的其他使用
import time
from selenium import webdriver
bro=webdriver.Chrome(executable_path='chromedriver.exe')
try:
browser=webdriver.Chrome()
browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
browser.switch_to.frame('iframssseResult')
except Exception as e:
print(e)
finally:
browser.close()
from selenium import webdriver
import json
import time
bro=webdriver.Chrome(executable_path='chromedriver.exe')
bro.get('http://www.cnblogs.com')
with open('cookie.json','r') as f:
cookie=json.load(f)
for i in cookie:
bro.add_cookie(i)
time.sleep(1)
bro.refresh()
time.sleep(1)
bro.refresh()
time.sleep(4)
bro.close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)