import requests from bs4 import BeautifulSoup # 发送请求 url = 'http://10.1.88.252:7000/' response = requests.get(url) # 解析HTML内容 soup = BeautifulSoup(response.text, 'html.parser') # 获取每本书的标签 books = soup.find_all('div', class_='book') # 遍历每本书 for book in books: # 获取书名 title = book.find('h2').text print('书名:', title) # 获取章节 chapters = book.find_all('div', class_='chapter') for chapter in chapters: # 获取章节标题 chapter_title = chapter.find('h3').text print('章节:', chapter_title) # 获取章节内容 chapter_content = chapter.find('div', class_='content').text print('内容:', chapter_content)代码报错AttributeError Traceback (most recent call last) <ipython-input-8-ff0836290511> in <module> 15 for book in books: 16 # 获取书名 ---> 17 title = book.find('h2').text 18 print('书名:', title) 19 AttributeError: 'NoneType' object has no attribute 'text'
时间: 2023-12-06 12:45:11 浏览: 234
这个错误通常是因为在查找HTML标签时未能找到对应的标签,导致返回了None,然后在None上调用.text属性时就会出现'NoneType' object has no attribute 'text'的错误。在你的代码中,你尝试使用find方法查找每本书的标题,但是有可能有一些书没有标题,所以就会出现找不到标题的情况。你可以在查找到标题之后,先判断一下是否为None,如果是None就跳过这本书即可。例如:
```
for book in books:
title_tag = book.find('h2')
if title_tag:
title = title_tag.text
print('书名:', title)
else:
continue
# 获取章节
chapters = book.find_all('div', class_='chapter')
for chapter in chapters:
# 获取章节标题
chapter_title = chapter.find('h3').text
print('章节:', chapter_title)
# 获取章节内容
chapter_content = chapter.find('div', class_='content').text
print('内容:', chapter_content)
```
相关问题
python爬取网页http://10.1.88.252:7000/获取每本书籍名称,书籍章节,章节内容
好的,这可以通过 Python 中的 requests 库和 BeautifulSoup 库来实现。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求
url = 'http://10.1.88.252:7000/'
response = requests.get(url)
# 解析HTML内容
soup = BeautifulSoup(response.text, 'html.parser')
# 获取每本书的标签
books = soup.find_all('div', class_='book')
# 遍历每本书
for book in books:
# 获取书名
title = book.find('h2').text
print('书名:', title)
# 获取章节
chapters = book.find_all('div', class_='chapter')
for chapter in chapters:
# 获取章节标题
chapter_title = chapter.find('h3').text
print('章节:', chapter_title)
# 获取章节内容
chapter_content = chapter.find('div', class_='content').text
print('内容:', chapter_content)
```
这个代码会依次输出每本书的名称、每个章节的标题和内容。你可以根据自己的需求修改这个代码。
username = 'your_username' password = 'your_password' session.get(login_url) login_data = { 'loginId': username, 'password2': password, 'appName': 'cnalichn', 'appEntrance': 'default', 'bizParams': '', 'styleType': 'auto', 'notLoadSsoView': 'false', 'notKeepLogin': 'true', 'isMobile': 'false', 'cssLink': 'https://g.alicdn.com/aliww/h5.openim.kit/1.0.28/css/kit.css,https://g.alicdn.com/aliww/h5.openim.kit/1.0.28/css/kit-theme.css,https://g.alicdn.com/aliww/h5.openim.kit/1.0.28/css/kit-login.css', 'notKeepUsername': 'true' } session.post(login_url, data=login_data) # 爬取千牛软件数据 qian_niu_url = 'https://qianliexian.taobao.com/?spm=a1z10.1-c-s.w5002-21491447486.2.5d6d4c5fKj9i5b' response = session.get(qian_niu_url) soup = BeautifulSoup(response.content, 'html.parser') data = soup.find_all('div', class_='item-info') for item in data: title = item.find('a', class_='item-title').text.strip() price = item.find('span', class_='item-price').text.strip() print(title, price)每行代码注释一下
当然,我会为你解释每行代码的作用和功能:
```python
# 导入需要的模块
import requests
from bs4 import BeautifulSoup
# 设置登录和获取数据的URL
login_url = 'https://login.taobao.com/member/login.jhtml'
qian_niu_url = 'https://qianliexian.taobao.com/?spm=a1z10.1-c-s.w5002-21491447486.2.5d6d4c5fKj9i5b'
# 创建一个session
session = requests.Session()
# 发送get请求到登录页面,获取cookie和其他必要信息
session.get(login_url)
# 设置登录需要的参数
username = 'your_username'
password = 'your_password'
login_data = {
'loginId': username,
'password2': password,
'appName': 'cnalichn',
'appEntrance': 'default',
'bizParams': '',
'styleType': 'auto',
'notLoadSsoView': 'false',
'notKeepLogin': 'true',
'isMobile': 'false',
'cssLink': 'https://g.alicdn.com/aliww/h5.openim.kit/1.0.28/css/kit.css,https://g.alicdn.com/aliww/h5.openim.kit/1.0.28/css/kit-theme.css,https://g.alicdn.com/aliww/h5.openim.kit/1.0.28/css/kit-login.css',
'notKeepUsername': 'true'
}
# 发送post请求进行登录
session.post(login_url, data=login_data)
# 发送get请求到目标页面,获取数据
response = session.get(qian_niu_url)
# 使用BeautifulSoup解析页面内容
soup = BeautifulSoup(response.content, 'html.parser')
# 获取需要的数据
data = soup.find_all('div', class_='item-info')
for item in data:
title = item.find('a', class_='item-title').text.strip()
price = item.find('span', class_='item-price').text.strip()
# 打印数据
print(title, price)
```
希望这些注释能够帮助你更好地理解这段代码。如果你还有任何疑问,请随时问我。
阅读全文
相关推荐












