用python爬取大连理工大学出版书籍信息

anwen4816 2024-6-19 1,222 6/19

 

一共俩种爬取方法下面显示一种出来

 

学习源码下载:下载源码

另一种方法在文件里自己下载即可

作者:gcoder

gcoder博客官网

https://gcoder5.com/

用python爬取大连理工大学出版书籍信息

复制代码
import requests from bs4 import BeautifulSoup # 是否继续爬取书籍的标识 is_continue = True print("干它") def get_current_page_books(url): """ 获取当前页面的所有书籍信息 :param url: :return: """ global is_continue current_response = requests.get(url) # 解析 HTML 内容 soup = BeautifulSoup(current_response.text, 'html.parser') # 找到所有书籍的标签 books = soup.find('div', {'class': 'left'}).find_all('div', {'class': 'dt'}) # 如果没有书籍 证明已经结束了 返回False 终止程序 if len(books) > 0: # 遍历每本书籍,提取书名、作者和 ISBN for book in books: book_name = book.find('a', {'class': 'h1'}).find('h1').text.strip() author_and_isbn = book.find('a').find('h2').text.strip() author_and_isbn_arr = author_and_isbn.split('ISBN:') author = author_and_isbn_arr[0][3:] isbn = author_and_isbn_arr[1] # 打印书籍信息 print('书名:', book_name) print('作者:', author) print('ISBN:', isbn) print('芭比啦爬取完了') else: is_continue = False   if __name__ == '__main__': # 发送 HTTP GET 请求 page = 'https://www.dutp.cn/index/search/index.html?keyword=%E6%95%B0%E5%AD%A6&type=2&active=0' response = requests.get(page) # 解析 HTML 内容 soup = BeautifulSoup(response.text, 'html.parser') # 找到总数 title = soup.find('div', {'class', 'title'}) total = title.find_all('span')[1].text.strip() print(f'与数学相关的书籍一共有{total}本') for pageNum in range(1, 100): if not is_continue: print(f'>>>> 第{pageNum-1}页的书籍为空,停止收集!') break print(f'>>>> 收集第{pageNum}页的书籍:') page_url = page + f'&page={pageNum}' get_current_page_books(page_url)
- THE END -

anwen4816

6月19日15:01

最后修改:2024年6月19日
1

作者:gcoder