本地
from bs4 import BeautifulSoup
if __name__ == '__main__':
fr = open("wl.html",'r',encoding="utf8")
soup=BeautifulSoup(fr,'lxml')
print(soup)
网络
from bs4 import BeautifulSoup
import requests
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}
if __name__ == '__main__':
url="https://www.cnblogs.com/zx125/p/11404486.html"
res=requests.get(url=url,headers=headers)
soup=BeautifulSoup(res.text,'lxml')
print(soup)
soup.tagname
直接返回第一个tag标签的内容
soup.find()
soup.find(tagname)效果和上面类似
soup.find(tagname,class_="")class_为tagname上的class内的属性
class_ id arr
双重定位 属性定位 但是只拿一个
soup.find_all()
用法和上面相同但是可以拿到满足条件的所有数据
soup.select()
select(‘某种选择器 #id .class 标签...‘),返回的是一个列表
它支持css的选择器
层级选择
soup.select(‘.zx > ul > li > a‘)一个>表示一个层级
soup.select(‘.zx > ul a‘)也可以这样写,一个空格代表以下的任意层级,并找到所有的a
获取标签的文本内容
soup.select(‘.zx > ul a‘).tagname.text/string/get_text()
text/get_text()获取标签下面所有的文本内容
string只获取直系的文本
获取标签中的属性值
.a["href"]
import requests
from bs4 import BeautifulSoup
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}
def work():
url="http://www.shicimingju.com/book/sanguoyanyi.html"
res=requests.get(url=url,headers=headers).text
#读取首页信息
soup=BeautifulSoup(res,"lxml")
#获取所有标题存在的a标签
titles=soup.select(".book-mulu > ul > li > a")
with open("./sangup.txt","w",encoding="utf8")as fw:
for i in titles:
#获取标题名称
title=i.text
#获取文章内容的url,并拼接成有效的请求链接
url_title="http://www.shicimingju.com"+i['href']
res2=requests.get(url=url_title,headers=headers).text
soup2=BeautifulSoup(res2,"lxml")
#获取每个章节的文章内容
content=soup2.find("div",class_="chapter_content").text
context_all=title+"\n"+content+"\n"
#将标题和文章内容写入本地文件
fw.write(context_all)
print(title+"写入成功")
if __name__ == '__main__':
work()原文:https://www.cnblogs.com/zx125/p/11405594.html