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

 运行结果:

 

现在可以看出是反爬。

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

相关内容

热门资讯

透视课程!wepokerh5破... 透视课程!wepokerh5破解(HHpoker控制牌)真是有辅助工具(哔哩哔哩)1、透视课程!we...
科普了解!四川长牌辅助工具教程... 科普了解!四川长牌辅助工具教程(辅助)728网络游戏一贯存在有辅助app(哔哩哔哩)四川长牌辅助工具...
透视秘籍!epoker底牌透视... 透视秘籍!epoker底牌透视(透视)哈糖大菠萝怎么挂(辅助)都是是真的有脚本(哔哩哔哩)1、哈糖大...
透视指引!wepoker插件程... 透视指引!wepoker插件程序激活码(WePoKer辅助挂)果然有辅助app(哔哩哔哩)1、上手简...
透视举措!德州局透视(透视)约... 透视举措!德州局透视(透视)约局吧辅助器(辅助)果然是真的有神器(哔哩哔哩)透视举措!德州局透视(透...
揭幕了解!鱼虾蟹作z弊视频教程... 揭幕了解!鱼虾蟹作z弊视频教程(辅助)悠闲娱乐切实存在有辅助器(哔哩哔哩)1、鱼虾蟹作z弊视频教程有...
透视窍要!we-poker是什... 透视窍要!we-poker是什么软件(WePoKer代打)竟然真的是有辅助攻略(哔哩哔哩)1、每一步...
透视秘籍!智星菠萝辅助(透视)... 透视秘籍!智星菠萝辅助(透视)约局吧开挂神器是真的吗(辅助)真是是真的有神器(哔哩哔哩)透视秘籍!智...
详细了解!樱花之盛辅助器下载(... 详细了解!樱花之盛辅助器下载(辅助)微乐天津麻将真是存在有辅助app(哔哩哔哩)1、樱花之盛辅助器下...
透视法子!hhpoker到底可... 透视法子!hhpoker到底可以作必弊码(WePoKer手机版)竟然是真的有辅助脚本(哔哩哔哩)hh...