我的账户
啄木鸟学院

专注软件测试菁英教育

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

python_赵梦冰_20210128

[复制链接]
Loading...学员认证 发表于 2021-1-28 20:21:21 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
本帖最后由 Loading... 于 2021-1-28 20:34 编辑

9:00——20:00
课堂笔记
import requests
from lxml import etree
#将爬虫伪装成浏览器
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
#使用get方法发出请求,然后接收响应
r = requests.get('https://www.baidu.com',headers=headers)
# r = requests.get('https://www.douban.com',headers=headers)
#设置编码集
r.encoding = 'uft-8'
#输出网页内容
# print(r.text)
#将字符串格式的网页转变为etree格式
selector = etree.HTML(r.text)
#获取图片地址
href = selector.xpath('//*[@id="s_lg_img"]/@src')[0]
print(href)
#下载图片
response = requests.get('https:'+href)
# print(response.content)
#保存图片
with open('result.png',mode='wb')as picture:
    picture.write(response.content)
import requests
from lxml import etree
# response = requests.get(r'D:\workspace\python\day10\sample.html')
# response.encoding = 'utf-8'
# print(response.text)
with open('D:\workspace\python\day10\sample.html','r')as file:
    page = file.read()
selector = etree.HTML(page)
ul = selector.xpath('//div/ul')
print(ul)
#获取所有的li元素
# lis = selector.xpath('//div/ul/li')
#获取第二个li
# li2 = selector.xpath('//div/ul/li')[1]
#
#多个同名元素编号从1开始
li2 = selector.xpath('//div/ul/li[2]')
print(li2)

#精确定位
li3 = selector.xpath('//div/ul/li[@class="item-inactive"]')
print(li3)

ass = selector.xpath('//div/ul/li/a')
print(ass)
#绝对路径
a4 = selector.xpath('//div/ul/li/a[@href="link4.html"]')
print(a4)
#通过文本来定位元素   相对路径
a4 = selector.xpath('//*[text()="fourth item"]')
print(a4)
回复

使用道具 举报

关注0

粉丝0

帖子26

发布主题
大家都在学
课堂讨论
一周热帖排行最近7x24小时热帖
关注我们
专注软件测试菁英教育

客服电话:17792550360

客服时间:9:00-21:00

卓目鸟学苑 - 专注软件测试菁英教育!( 陕ICP备20001493号-1 )

版权所有 © 西安菁英教育科技有限公司 2023-2026