import//u0020time//nimport//u0020random//nimport//u0020re//nfrom//u0020selenium//u0020import//u0020webdriver//nfrom//u0020selenium.webdriver.common.keys//u0020import//u0020Keys//n//n#//u0020创建浏览器对象//ndriver//u0020=//u0020webdriver.Chrome()//n//n#//u0020打开b站首页//ndriver.get(/'https://miaosha.jd.com//')//n//n#//u0020输入搜索关键词//nsearch_input//u0020=//u0020driver.find_element_by_xpath(/'//[@id='key']/')//nsearch_input.send_keys(/'人工智能图书/')//ntime.sleep(0.1)//n//n#//u0020按回车键进行搜索//nsearch_input.send_keys(Keys.ENTER)//ntime.sleep(3)//n//n#//u0020获取当前窗口句柄//ncurrent_window//u0020=//u0020driver.current_window_handle//n#//u0020切换到新打开的窗口//nfor//u0020window_handle//u0020in//u0020driver.window_handles://n//u0020//u0020if//u0020window_handle//u0020!=//u0020current_window://n//u0020//u0020//u0020//u0020driver.switch_to.window(window_handle)//n//u0020//u0020//u0020//u0020break//n//n#//u0020获取搜索结果页面的所有书名、价格、评论信息//nbooks//u0020=//u0020driver.find_elements_by_xpath(/'//[@id='J_goodsList']/ul/li/')//n//n#//u0020遍历每本书的信息//nfor//u0020book//u0020in//u0020books://n//u0020//u0020try://n//u0020//u0020//u0020//u0020#//u0020获取书名、价格、评论信息//n//u0020//u0020//u0020//u0020book_name//u0020=//u0020book.find_element_by_xpath(/'.//div/div[3]/')//n//u0020//u0020//u0020//u0020price//u0020=//u0020book.find_element_by_xpath(/'.//div/div[2]/')//n//n//u0020//u0020//u0020//u0020#//u0020判断评论信息是否包含/'评价/'两个字//n//u0020//u0020//u0020//u0020comment//u0020=//u0020book.find_element_by_xpath(/'.//div/div[4]/')//n//u0020//u0020//u0020//u0020if//u0020/'评价/'//u0020not//u0020in//u0020comment.text://n//u0020//u0020//u0020//u0020//u0020//u0020comment//u0020=//u0020book.find_element_by_xpath(/'.//div/div[5]/')//n//n//u0020//u0020//u0020//u0020print(/'书名://u0020/'//u0020+//u0020book_name.text)//u0020#//u0020打印书名//n//n//u0020//u0020//u0020//u0020#//u0020获取价格信息//n//u0020//u0020//u0020//u0020price_text//u0020=//u0020price.text//n//u0020//u0020//u0020//u0020price_match//u0020=//u0020re.search(r/'¥(/d+/./d+)/',//u0020price_text)//n//u0020//u0020//u0020//u0020if//u0020price_match://n//u0020//u0020//u0020//u0020//u0020//u0020price_value//u0020=//u0020price_match.group(1)//n//u0020//u0020//u0020//u0020//u0020//u0020print(/'价格://u0020/'//u0020+//u0020price_value)//u0020#//u0020打印价格//n//u0020//u0020//u0020//u0020else://n//u0020//u0020//u0020//u0020//u0020//u0020print(/'未找到价格信息/')//n//n//u0020//u0020//u0020//u0020#//u0020使用正则表达式提取评论数//n//u0020//u0020//u0020//u0020comment_match//u0020=//u0020re.search(r/'(/d+)/',//u0020comment.text)//n//u0020//u0020//u0020//u0020if//u0020comment_match://n//u0020//u0020//u0020//u0020//u0020//u0020comment_text//u0020=//u0020comment_match.group(1)//n//u0020//u0020//u0020//u0020//u0020//u0020print(/'评论数://u0020/'//u0020+//u0020comment_text)//u0020#//u0020打印评论数//n//u0020//u0020//u0020//u0020else://n//u0020//u0020//u0020//u0020//u0020//u0020print(/'未找到评论数/')//n//n//u0020//u0020//u0020//u0020#//u0020点击书名打开详情页面//n//u0020//u0020//u0020//u0020book_name.click()//n//u0020//u0020//u0020//u0020time.sleep(10)//n//n//u0020//u0020//u0020//u0020#//u0020获取当前窗口句柄//n//u0020//u0020//u0020//u0020current_window//u0020=//u0020driver.current_window_handle//n//u0020//u0020//u0020//u0020#//u0020切换到详情页面的窗口//n//u0020//u0020//u0020//u0020window_handle//u0020=//u0020driver.window_handles[1]//n//u0020//u0020//u0020//u0020driver.switch_to.window(window_handle)//n//n//u0020//u0020//u0020//u0020#//u0020获取当前页面的URL//n//u0020//u0020//u0020//u0020current_url//u0020=//u0020driver.current_url//n//n//u0020//u0020//u0020//u0020#//u0020查找出版社信息//n//u0020//u0020//u0020//u0020publisher//u0020=//u0020None//n//u0020//u0020//u0020//u0020possible_xpaths//u0020=//u0020[//n//u0020//u0020//u0020//u0020//u0020//u0020/'//[@id='detail']/div[2]/div[1]/div[1]/ul/li/a/',//n//u0020//u0020//u0020//u0020//u0020//u0020/'//[@id='detail']/div[2]/div[1]/div[1]/ul/li[2]/a/',//n//u0020//u0020//u0020//u0020//u0020//u0020/'//[@id='detail']/div[2]/div[1]/div[1]/ul/li[1]/a[1]/'//n//u0020//u0020//u0020//u0020]//n//u0020//u0020//u0020//u0020for//u0020xpath//u0020in//u0020possible_xpaths://n//u0020//u0020//u0020//u0020//u0020//u0020publishers//u0020=//u0020driver.find_elements_by_xpath(xpath)//n//u0020//u0020//u0020//u0020//u0020//u0020if//u0020publishers://n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020publisher//u0020=//u0020publishers[0]//n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020if//u0020/'出版社/'//u0020in//u0020publisher.text://n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020break//n//n//u0020//u0020//u0020//u0020if//u0020publisher://n//u0020//u0020//u0020//u0020//u0020//u0020if//u0020/'出版社/'//u0020in//u0020publisher.text://n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020print(/'出版社://u0020/'//u0020+//u0020publisher.text)//n//u0020//u0020//u0020//u0020//u0020//u0020else://n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020print(/'未找到出版社信息/')//n//u0020//u0020//u0020//u0020else://n//u0020//u0020//u0020//u0020//u0020//u0020print(/'未找到出版社信息/')//n//u0020//u0020//u0020//u0020#//u0020查找出版年份信息//n//u0020//u0020//u0020//u0020publish_year//u0020=//u0020None//n//u0020//u0020//u0020//u0020possible_xpaths//u0020=//u0020[//n//u0020//u0020//u0020//u0020//u0020//u0020/'//[@id='detail']/div[2]/div[1]/div[1]/ul/li[8]/',//n//u0020//u0020//u0020//u0020//u0020//u0020/'//[@id='detail']/div[2]/div[1]/div[1]/ul/li[6]/',//n//u0020//u0020//u0020//u0020//u0020//u0020/'//[@id='detail']/div[2]/div[1]/div[1]/ul/li[5]/'//n//u0020//u0020//u0020//u0020]//n//u0020//u0020//u0020//u0020for//u0020xpath//u0020in//u0020possible_xpaths://n//u0020//u0020//u0020//u0020//u0020//u0020publish_years//u0020=//u0020driver.find_elements_by_xpath(xpath)//n//u0020//u0020//u0020//u0020//u0020//u0020if//u0020publish_years://n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020publish_year//u0020=//u0020publish_years[0]//n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020if//u0020/'出版时间/'//u0020in//u0020publish_year.text://n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020break//n//n//u0020//u0020//u0020//u0020if//u0020publish_year://n//u0020//u0020//u0020//u0020//u0020//u0020publish_year_text//u0020=//u0020re.search(r/':(.+)/',//u0020publish_year.text)//n//u0020//u0020//u0020//u0020//u0020//u0020if//u0020publish_year_text://n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020publish_year_value//u0020=//u0020publish_year_text.group(1)//n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020print(/'出版年份://u0020/'//u0020+//u0020publish_year_value)//n//u0020//u0020//u0020//u0020//u0020//u0020else://n//u0020//u0020//u0020//u0020//u0020//u0020//u0020//u0020print(/'未找到出版年份信息/')//n//u0020//u0020//u0020//u0020else://n//u0020//u0020//u0020//u0020//u0020//u0020print(/'未找到出版年份信息/')//n//u0020//u0020//u0020//u0020#//u0020Click//u0020on//u0020the//u0020/'商品评价/'//u0020button//n//u0020//u0020//u0020//u0020shop_button//u0020=//u0020driver.find_elements_by_xpath(/'//[@id='detail']/div[1]/ul/li[5]/')[0]//n//u0020//u0020//u0020//u0020shop_button.click()//n//u0020//u0020//u0020//u0020time.sleep(5)//n//n//u0020//u0020//u0020//u0020#//u0020爬取并输出评价信息(好评中评差评数目)//n//u0020//u0020//u0020//u0020good_comments//u0020=//u0020driver.find_elements_by_xpath(/'//[@id='comment']/div[2]/div[2]/div[1]/ul/li[5]/a/em/')//n//u0020//u0020//u0020//u0020for//u0020comment//u0020in//u0020good_comments://n//u0020//u0020//u0020//u0020//u0020//u0020print(/'好评:/'//u0020+//u0020comment.text.strip(/'()/'))//n//u0020//u0020//u0020//u0020medium_comments//u0020=//u0020driver.find_elements_by_xpath(/'//[@id='comment']/div[2]/div[2]/div[1]/ul/li[6]/a/em/')//n//u0020//u0020//u0020//u0020for//u0020comment//u0020in//u0020medium_comments://n//u0020//u0020//u0020//u0020//u0020//u0020print(/'中评:/'//u0020+//u0020comment.text.strip(/'()/'))//n//u0020//u0020//u0020//u0020bad_comments//u0020=//u0020driver.find_elements_by_xpath(/'//[@id='comment']/div[2]/div[2]/div[1]/ul/li[7]/a/em/')//n//u0020//u0020//u0020//u0020for//u0020comment//u0020in//u0020bad_comments://n//u0020//u0020//u0020//u0020//u0020//u0020print(/'差评:/'//u0020+//u0020comment.text.strip(/'()/'))//n//n//u0020//u0020//u0020//u0020#//u0020关闭当前窗口,回到搜索结果页面//n//u0020//u0020//u0020//u0020driver.close()//n//u0020//u0020//u0020//u0020driver.switch_to.window(driver.window_handles[0])//n//n//u0020//u0020//u0020//u0020#//u0020生成随机的点击延迟时间//n//u0020//u0020//u0020//u0020time.sleep(random.randint(1,//u00205))//n//u0020//u0020except//u0020Exception//u0020as//u0020e://n//u0020//u0020//u0020//u0020print(str(e))//n//u0020//u0020print(/'--------------------------------------------------/')//u0020#//u0020使用分隔符分隔每本图书信息//n//n#//u0020关闭浏览器//ndriver.quit()//n

京东人工智能图书爬虫 - 获取书名、价格、评论、出版社和出版年份

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

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