栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python【爬虫小练习】三国演绎文章链接

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python【爬虫小练习】三国演绎文章链接

import requests
#导入beautifulsoup 包
from bs4 import BeautifulSoup

# 爬取的url
url="https://www.shicimingju.com/book/sanguoyanyi.html"
#UA反扒
head={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74"
}
#向url发起get请求
re=requests.get(url=url,headers=head)
#处理页面乱码
re.encoding=re.apparent_encoding
#对网页源代码进行处理
# >>>>>>>>>html.parser 表示指定处理数据为html页面<<<<<<<<<<<
page=BeautifulSoup(re.text,'html.parser')
#从page对象中查找数据
# 1. find(标签,属性=值) 查找第一个
# 2. find_all(标签,属性=值)查找所有
# mid=page.find('div',class_='book-mulu')
mid=page.find('div',attrs={'class':'book-mulu'})
lis=mid.find_all('li')
u='https://www.shicimingju.com'
for ind in lis:
    a=ind.find('a')
    content=a.text       # -- 获取每一个a标签内容
    ur=u+a.get('href')   #-- 表示a标签'href'属性的内容--并且做一个字符串拼接
    print(content,ur)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/887555.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号