以下是一个简单的python爬取当当图书网数据脚本,使用的是requests和BeautifulSoup库:

import requests
from bs4 import BeautifulSoup

# 设置请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}

# 构造url和params
url = 'http://search.dangdang.com'
params = {
    'key': 'python',  # 搜索关键词
    'act': 'input',  # 搜索类型
    'sort_type': 'sort_default',  # 排序方式
    'page_index': '1',  # 页码
}

# 发送get请求
response = requests.get(url, headers=headers, params=params)

# 解析html内容
soup = BeautifulSoup(response.text, 'html.parser')

# 找到所有图书信息的div
divs = soup.find_all('div', {'class': 'book_info'})

# 遍历每个图书信息的div
for div in divs:
    # 获取书名
    book_title = div.find('a', {'class': 'pic'}).get('title')
    # 获取作者
    book_author = div.find('div', {'class': 'publisher_info'}).find_all('a')[0].text
    # 获取出版社
    book_publisher = div.find('div', {'class': 'publisher_info'}).find_all('a')[1].text
    # 获取价格
    book_price = div.find('div', {'class': 'price'}).find('span', {'class': 'search_now_price'}).text
    # 输出图书信息
    print('书名:', book_title)
    print('作者:', book_author)
    print('出版社:', book_publisher)
    print('价格:', book_price)
    print('-----------------')

这个脚本会爬取当当图书网上关键词为python的图书信息,包括书名、作者、出版社和价格,并将其打印输出。你可以根据需要进行修改和扩展。

请帮我写一个python爬取当当图书网数据脚本

原文地址: https://www.cveoy.top/t/topic/Oid 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录