工作任务和目标:用户输入一个图书名称,然后程序自动从当当网批量获取图书信息 查看相关元素在源代码中的位置: 第一步:在deepseek中输入提示词: 你是一个Python爬虫专家,一步步的思考,完成以下网页爬取的Python脚本任务: 用户输入一个关键词,接受这个关键词,保存为变量{book}; 在F:\aivideo文件夹里面新建一个Excel文件:{book}.xlsx 打开网页:https://search.dangdang.com/?key={book}&act=input&page_index={pagenumber}&sort_type=sort_sale_amt_desc#J_tab ({pagenumber}这个变量的值是从1到100); 网页的编码为charset=GB2312; 解析网页的源代码,并打印输出; 定位class="bigimg" 的ul 标签; 在定位ul 标签内定位所有的li 标签, 在li 标签内定位class="pic"的a标签,提取其title属性值,作为图书标题,保存到{book}.xlsx的第1列; 在li 标签内定位class="detail"的p标签,提取其文字内容,作为图书简介,保存到{book}.xlsx的第2列; 在li 标签内定位class="price">的p标签,然后定期p标签中 class="search_now_price"的span标签,提取其内容,作为图书价格,保存到{book}.xlsx的第3列; 在li 标签内定位name="itemlist-review"的a标签,提取其文字内容,作为图书评论数量,保存到{book}.xlsx的第4列; 在li 标签内定位 class="search_book_author"的p标签,定位p标签中的第1个a标签,提取其 title属性值,作为图书作者,保存到{book}.xlsx的第5列;定位p标签中的第2个a标签,提取其 title属性值,作为图书出版社,保存到{book}.xlsx的第6列;定位p标签中的第2个span标签,提取其内容,作为出版时间,保存到{book}.xlsx的第7列; 注意: 每一步都要输出相关信息到屏幕; 设置请求头,来应对反爬虫机制; 每解析一个网页后,随机等待1-10秒; 如果某个元素不存在,就跳过这个数据点。 DataFrame.append 方法在 pandas 1.4.0 版本中已经被弃用,并且在后续版本中被移除。为了解决这个问题,我们可以使用 concat 函数来代替 append 所有的代码整合在一起,不要一段一段的输出; 第二步,查看审阅deepseek生成的Python代码: import random import requests from bs4 import BeautifulSoup import pandas as pd # 设置请求头 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'} # 用户输入关键词 book = input("请输入关键词: ") # 新建Excel文件路径 excel_file_path = f"F:\\aivideo\\{book}.xlsx" # 初始化DataFrame df = pd.DataFrame(columns=['标题', '简介', '价格', '评论数量', '作者', '出版社', '出版时间']) # 遍历页面 for pagenumber in range(1, 101): # 打开网页 url = f"https://search.dangdang.com/?key={book}&act=input&page_index={pagenumber}&sort_type=sort_sale_amt_desc#J_tab" print(f"正在打开网页: {url}") # 发送HTTP请求 response = requests.get(url, headers=headers) response.encoding = 'GB2312' # 设置编码为GB2312 # 解析网页 soup = BeautifulSoup(response.text, 'html.parser') # 定位class="bigimg"的ul标签 ul_tag = soup.find('ul', class_='bigimg') # 如果ul标签存在,定位所有的li标签 if ul_tag: li_tags = ul_tag.find_all('li') # 遍历li标签并提取信息 for li in li_tags: try: # 提取图书信息 title = li.find('a', class_='pic').get('title') description = li.find('p', class_='detail').text.strip() price = li.find('p', class_='price').find('span', class_='search_now_price').text.strip() review_count = li.find('a', attrs={'name': 'itemlist-review'}).text.strip() author = li.find('p', class_='search_book_author').find_all('a')[0].get('title') publisher = li.find('p', class_='search_book_author').find_all('a')[1].get('title') publish_time = li.find('p', class_='search_book_author').find_all('span')[1].text.strip() # 保存到DataFrame df = pd.concat([df, pd.DataFrame([[title, description, price, review_count, author, publisher, publish_time]], columns=['标题', '简介', '价格', '评论数量', '作者', '出版社', '出版时间'])], ignore_index=True) except AttributeError: print("跳过一个数据点") continue # 随机等待 time.sleep(random.randint(1, 10)) # 保存到Excel文件 df.to_excel(excel_file_path, index=False) print(f"数据已保存到: {excel_file_path}") 第三步,打开visual studio code软件,新建一个py文件,将Python代码复制到这个文件中,按下F5键运行程序: 程序运行结果: |
|