以下是一个使用Python爬虫爬取当当图书网数据的示例代码:

import requests
from bs4 import BeautifulSoup

def scrape_dangdang_books(url):
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
    books = []
    for book in soup.find_all('div', class_='name'):
        title = book.find('a').text.strip()
        author = book.find('span', class_='t1').text.strip()
        publisher = book.find('span', class_='t2').text.strip()
        books.append({'title': title, 'author': author, 'publisher': publisher})
    return books

url = 'http://book.dangdang.com/'
books = scrape_dangdang_books(url)
print(books)

该代码使用了requests和BeautifulSoup模块来获取和解析当当图书网的HTML页面。它遍历页面中的每个书籍,并提取书名、作者和出版社信息,最终以字典列表的形式返回所有书籍的信息。

要使用该代码,只需将要爬取的当当图书网页面的URL传递给scrape_dangdang_books()函数即可。在该示例中,我们传递了当当图书网的主页URL。

Python爬虫实战:抓取当当网图书数据

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

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