爬虫案例(读书网)(下)
创始人
2024-12-28 13:36:52
0

上篇链接:

CSDN-读书网icon-default.png?t=N7T8https://mp.csdn.net/mp_blog/creation/editor/139306808

可以看见基本的全部信息:如(author、bookname、link.....)

 写下代码如下:

import requests from bs4 import BeautifulSoup from lxml import etree  headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'} link="https://www.dushu.com/" r=requests.get(link,headers=headers) r.encoding='utf-8'  soup=BeautifulSoup(r.text,'lxml') house_list=soup.find_all('div',class_="border books-center") html=etree.HTML(r.text)     # name=html.xpath('//div[@class="property-content-title"]/h3/text()') # for house in house_list: #     name=soup.find('div',class_="nlist").a.strong.text() # #     print(name) name=html.xpath('//div[@class="bookname"]/a/text()') author=html.xpath('//div[@class="bookauthor"]/text()') # href=html.xpath('//div[@class="nlist"]/div/ul/li/a/@href')  #print(type(author)) for i,o in zip(name,author):     print('<<'+i+'>>',o)

运行结果:

接下来添加link链接:

可以看见现在网站设置了反爬,我们现在通过检查浏览器能正常爬取还是有反爬:

# 请用 python+selenium  爬取 XXX 网站上的所有a链接的 href属性并访问,输出访问地址和状态码 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import requests  driver = webdriver.Chrome() # 这里以百度为例 driver.get("https://www.dushu.com/")  wait = WebDriverWait(driver, 10) links = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//a")))  # 遍历所有的链接元素,并输出href属性值 for link in links:     href = link.get_attribute("href")     if href.startswith("http"):         response = requests.get(href)         print(href, response.status_code)     else:         link.click()         print(driver.current_url, driver.execute_script('return document.readyState'),               requests.get(driver.current_url).status_code)  # 关闭浏览器 driver.quit()

 运行结果:

 

现在可以看出是反爬。

最后我们的解析反爬,在下一篇文章详细介绍几个方法和使用效果。 

相关内容

热门资讯

第九分钟辅助!天天爱柳州辅助器... 天天爱柳州辅助器是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我微...
热门推荐!微信小程序蜀山辅助器... 热门推荐!微信小程序蜀山辅助器免费下载(辅助)外挂辅助开挂插件(有挂秘诀)-哔哩哔哩;最新版2026...
第4分钟普及!奇迹陕西辅助器(... 第4分钟普及!奇迹陕西辅助器(透视)原来真的是有挂脚本(推荐开挂软件);奇迹陕西辅助器免费下载原版,...
第八分钟讲解!越乡游金花辅助,... 第八分钟讲解!越乡游金花辅助,九酷众游辅助,细节方法(有挂实锤)-哔哩哔哩1、下载安装好越乡游金花辅...
透视软件!浙江宝宝游戏辅助工具... 透视软件!浙江宝宝游戏辅助工具(辅助)外挂开挂辅助插件(了解有挂)-哔哩哔哩;是一款可以让一直输的玩...
第5分钟发现!桃乐甘肃麻将下载... 《第5分钟发现!桃乐甘肃麻将下载辅助器(辅助)原来有挂软件(必看开挂工具)》 桃乐甘肃麻将下载辅助器...
第8分钟了解!河洛刚次辅助,温... 第8分钟了解!河洛刚次辅助,温州茶苑辅助软件,详细教程(有挂攻略)-哔哩哔哩;无需打开直接搜索加薇1...
热点讨论!新二号辅助软件下载(... 热点讨论!新二号辅助软件下载(辅助)外挂辅助开挂软件(有挂教程)-哔哩哔哩;是一款可以让一直输的玩家...
8分钟解说!网易亲友圈辅助(开... 8分钟解说!网易亲友圈辅助(开挂)原来有挂平台(必看开挂安装);小薇(透视辅助)致您一封信;亲爱网易...
第5分钟开挂!如何在哈灵上辅助... 第5分钟开挂!如何在哈灵上辅助,决战卡五星辅助看牌器,解说技巧(有挂方略)-哔哩哔哩 了解更多开挂安...