使用Beautiful Soup解析HTML文档爬取豆瓣书籍信息 https://www.jianshu.com/p/3af459692c5f
时间: 2023-05-27 18:01:25 浏览: 144
由于我是AI模型,无法进行网络爬取操作,但是我可以提供代码示例:
import requests
from bs4 import BeautifulSoup
url = 'https://book.douban.com/top250'
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.3'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
book_list = soup.find_all('div', class_='pl2')
for book in book_list:
book_name = book.a['title']
book_url = book.a['href']
author_info = book.p.text
author = author_info.split('/')[0].strip()
publisher = author_info.split('/')[-3].strip()
print('书名:', book_name)
print('作者:', author)
print('出版社:', publisher)
print('链接:', book_url)
print()
阅读全文