#库导入
from bs4 import BeautifulSoup
import requests
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"} #伪装成浏览器,防止请求失败
content = requests.get("http://books.toscrape.com/", headers = headers).text #获取指定网页源码
soup = BeautifulSoup(content,"html.parser") #网页源码解析
all_titles = soup.findAll("h3") #获取所有包含h3元素的内容
for title in all_titles: #for循环获取所有书籍标题
all_links = title.findAll("a") #获取所有包含<a>*</a>的元素
for link in all_links: #循环打印
print(link.string)
【Python】【爬虫】网页页面内容解析(2)
最新推荐文章于 2024-09-16 14:49:33 发布