python爬虫爬取某图书网页实例
    		       		warning:
    		            这篇文章距离上次修改已过427天,其中的内容可能已经有所变动。
    		        
        		                
                
import requests
from bs4 import BeautifulSoup
 
# 图书详情页URL
book_url = 'https://book.douban.com/subject/1084336/discussion/new?start=200'
 
# 发送HTTP请求
response = requests.get(book_url)
 
# 检查请求是否成功
if response.status_code == 200:
    # 使用BeautifulSoup解析页面
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 提取图书名称
    book_name = soup.find('h1', class_='title').text
    print(f'图书名称: {book_name}')
    
    # 提取评论数
    comments_count = soup.find('span', class_='comments-count').text
    print(f'评论数: {comments_count}')
    
    # 提取评分
    rating_score = soup.find('strong', class_='ll rating-score').text
    print(f'评分: {rating_score}')
    
    # 提取作者信息
    author_info = soup.find('div', class_='info').text
    print(f'作者信息: {author_info}')
    
    # 提取书评摘要
    comment_summary = soup.find_all('div', class_='comment-summary')
    for summary in comment_summary:
        print(summary.text)
else:
    print('请求失败')这段代码使用了requests库来发送HTTP请求,使用BeautifulSoup库来解析页面,并提取了图书名称、评论数、评分、作者信息和书评摘要。这是一个简单的网页爬取示例,适合作为学习如何使用Python进行网页爬取的起点。
评论已关闭