爬虫案例(读书网)(下)
创始人
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()

 运行结果:

 

现在可以看出是反爬。

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

相关内容

热门资讯

有消息称!了解科技南通长牌辅助... 有消息称!了解科技南通长牌辅助神器!原来存在有开挂辅助器(有挂方针)-哔哩哔哩1、了解科技南通长牌辅...
1分钟辅助器外挂!wejoke... 1分钟辅助器外挂!wejoker辅助脚本,wepoker透视底牌(有挂分享)-哔哩哔哩1、wepok...
8分钟辅助器外挂!wepoke... 8分钟辅助器外挂!wepoker正确养号方法,wpk透视辅助(有挂解密)-哔哩哔哩1、wepoker...
出现新变化!威信闲来辅助神器a... 出现新变化!威信闲来辅助神器app!都是是有开挂辅助软件(有挂实锤)-哔哩哔哩威信闲来辅助神器app...
9分钟辅助器外挂!wepoke... 9分钟辅助器外挂!wepoker辅助软件视频,wepoker私局辅助(有挂细节)-哔哩哔哩1、上手简...
不少玩家反映!epoker透视... 不少玩家反映!epoker透视底牌!本来真的有开挂辅助脚本(有挂方法)-哔哩哔哩1、超多福利:超高返...
第6分钟辅助器外挂!wepok... 第6分钟辅助器外挂!wepokerplus透视脚本免费,WePoKer辅助器(有挂秘诀)-哔哩哔哩1...
总结辅助挂!龙岩闲游安卓脚本!... 总结辅助挂!龙岩闲游安卓脚本!竟然存在有开挂辅助器(有挂解惑)-哔哩哔哩一、龙岩闲游安卓脚本可以开透...
五分钟辅助器外挂!wpk辅助哪... 五分钟辅助器外挂!wpk辅助哪里买,wejoker内置辅助(有挂秘笈)-哔哩哔哩1、实时wejoke...
最新消息!中至抚州数刀辅助器!... 最新消息!中至抚州数刀辅助器!总是是真的有开挂辅助神器(确实有挂)-哔哩哔哩在进入中至抚州数刀辅助器...