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)