当前位置: 首页 > news >正文

python做网站多么镇江网站建设制作公司

python做网站多么,镇江网站建设制作公司,四川网站开发哪家好,wordpress 筛选插件博客正文(包含详细注释) 引言 在爬虫技术领域,处理动态加载的网页内容常常是一项挑战,尤其是对于那些通过用户滚动或其他交互动态加载更多内容的网站。本文将介绍如何结合使用Selenium和Scrapy来有效处理这类网页。 初探Seleni…

博客正文(包含详细注释)

引言

在爬虫技术领域,处理动态加载的网页内容常常是一项挑战,尤其是对于那些通过用户滚动或其他交互动态加载更多内容的网站。本文将介绍如何结合使用Selenium和Scrapy来有效处理这类网页。

初探Selenium与Scrapy的结合

首先,我们探索如何使用Selenium在Scrapy中间件中处理动态加载内容的网页。关键在于模拟用户滚动行为,以加载并捕获所有内容。

def process_response(self, request, response, spider):driver = spider.driver# 检查请求的URL是否在我们的目标列表中if request.url in spider.page_url:driver.get(request.url)  # 使用Selenium打开页面# 等待页面初步加载完成time.sleep(3)  # 示例等待时间,可能需要根据实际页面调整# 获取当前页面的高度last_height = driver.execute_script("return document.body.scrollHeight")while True:# 滚动到页面底部driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")# 等待页面可能出现的新内容加载time.sleep(3)  # 重新获取新的页面高度new_height = driver.execute_script("return document.body.scrollHeight")# 如果高度不再改变,说明到达了页面底部if new_height == last_height:breaklast_height = new_height  # 更新高度,用于下次比较# 获取完整的页面源代码text = driver.page_source# 创建新的HtmlResponse并返回return HtmlResponse(url=request.url, body=text, encoding='utf-8', request=request)# 如果URL不在目标列表中,返回原始响应return response
完整的Scrapy爬虫实例

下面是一个使用Selenium和Scrapy爬取网易新闻的示例。

import scrapy
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.chrome.options import Optionsclass WySpider(scrapy.Spider):name = "wy"  # 爬虫名称start_urls = ["https://news.163.com/domestic/"]  # 起始URL# Selenium配置opt = Options()opt.add_argument('--headless')  # 添加headless参数,指定浏览器在无界面模式下运行,即没有用户界面或可视化界面的情况下。opt.add_argument('--disable-gpu')  # 禁用GPU加速opt.add_argument('--window-size=4000,1600')  # 设置浏览器窗口大小opt.add_experimental_option('excludeSwitches', ['enable-automation'])  # 防止网站识别出自动化测试driver = Chrome(options=opt)  # 创建Chrome驱动href_index = [1, 2]  # 指定要处理的链接索引page_url = []  # 存储目标URL地址# 处理起始URL的响应def parse(self, resp, **kwargs):# 提取链接href_list = resp.xpath('/html/body/div/div[3]/div[2]/div[2]/div/ul/li/a/@href').extract()for i in range(len(href_list)):if i in self.href_index:# 如果链接在指定索引中,添加到目标列表并发起请求self.page_url.append(href_list[i])yield scrapy.Request(url=href_list[i], callback=self.parse_detail)# 处理获取的新闻类别链接def parse_detail(self, resp, **kwargs):# 提取详细页面的链接detail_url = resp.xpath('/html/body/div/div[3]/div[3]/div[1]/div[1]/div/ul/li/div/div/div/div[1]/h3/a/@href').extract()for url in detail_url:# 对每个详细新闻链接发起请求yield scrapy.Request(url=url, callback=self.parse_detail_content)# 提取并处理新闻详细内容def parse_detail_content(self, resp, **kwargs):# 提取新闻标题title = resp.xpath('//*[@id="contain"]/div[2]/h1/text()').extract_first()# 提取新闻内容con = resp.xpath('//*[@id="content"]/div[2]//text()').extract()con = ''.join(con).strip()data = {'title': title, 'con': con}  # 封装提取的数据print(data)  # 打印数据yield data  # 返回提取的数据
使用场景

这种结合Selenium和Scrapy的方法适用于需要处理动态加载内容的网页,如新闻网站、社交媒体平台等。

结语

通过结合Selenium和Scrapy,我们可以有效地处理那些动态加载内容的网页,这对于数据抓取和网络爬虫项目至关重要。希望这篇文章能够帮助您在面对类似的挑战时,有所启发和帮助。



文章转载自:
http://pondfish.Lgnz.cn
http://gibblegabble.Lgnz.cn
http://jammy.Lgnz.cn
http://misanthropize.Lgnz.cn
http://butterbur.Lgnz.cn
http://aboil.Lgnz.cn
http://distortedness.Lgnz.cn
http://critter.Lgnz.cn
http://clotted.Lgnz.cn
http://indolently.Lgnz.cn
http://rigging.Lgnz.cn
http://prophesy.Lgnz.cn
http://exultantly.Lgnz.cn
http://rickettsia.Lgnz.cn
http://albumenize.Lgnz.cn
http://udderless.Lgnz.cn
http://sandhi.Lgnz.cn
http://kithara.Lgnz.cn
http://xanthophyl.Lgnz.cn
http://cubicule.Lgnz.cn
http://telelens.Lgnz.cn
http://hamah.Lgnz.cn
http://platitudinarian.Lgnz.cn
http://anaphylaxis.Lgnz.cn
http://corticosteroid.Lgnz.cn
http://indistinguishable.Lgnz.cn
http://monoestrous.Lgnz.cn
http://cariogenic.Lgnz.cn
http://potence.Lgnz.cn
http://ardeidae.Lgnz.cn
http://sombrero.Lgnz.cn
http://vacillating.Lgnz.cn
http://exopodite.Lgnz.cn
http://gertcha.Lgnz.cn
http://parity.Lgnz.cn
http://reductant.Lgnz.cn
http://predominance.Lgnz.cn
http://irresoluble.Lgnz.cn
http://sericate.Lgnz.cn
http://wiseass.Lgnz.cn
http://buckskin.Lgnz.cn
http://illuminaten.Lgnz.cn
http://shucks.Lgnz.cn
http://turrethead.Lgnz.cn
http://cruiser.Lgnz.cn
http://container.Lgnz.cn
http://router.Lgnz.cn
http://desist.Lgnz.cn
http://selenodont.Lgnz.cn
http://lithesome.Lgnz.cn
http://dong.Lgnz.cn
http://emblematology.Lgnz.cn
http://simious.Lgnz.cn
http://tryptophane.Lgnz.cn
http://murk.Lgnz.cn
http://prosodical.Lgnz.cn
http://raising.Lgnz.cn
http://crawler.Lgnz.cn
http://laryngitis.Lgnz.cn
http://attitude.Lgnz.cn
http://nereis.Lgnz.cn
http://psoralen.Lgnz.cn
http://celiac.Lgnz.cn
http://nd.Lgnz.cn
http://cheerio.Lgnz.cn
http://deckhead.Lgnz.cn
http://gedankenexperiment.Lgnz.cn
http://ibsenian.Lgnz.cn
http://mens.Lgnz.cn
http://pinwale.Lgnz.cn
http://heffalump.Lgnz.cn
http://chapter.Lgnz.cn
http://endoarteritis.Lgnz.cn
http://mild.Lgnz.cn
http://postpartum.Lgnz.cn
http://ineluctability.Lgnz.cn
http://insurer.Lgnz.cn
http://monomorphemic.Lgnz.cn
http://hooly.Lgnz.cn
http://railwayman.Lgnz.cn
http://legerdemain.Lgnz.cn
http://amyl.Lgnz.cn
http://toboggan.Lgnz.cn
http://inkiness.Lgnz.cn
http://unequalable.Lgnz.cn
http://indurate.Lgnz.cn
http://shining.Lgnz.cn
http://nighted.Lgnz.cn
http://klutz.Lgnz.cn
http://gk97.Lgnz.cn
http://ticktacktoe.Lgnz.cn
http://bronchitic.Lgnz.cn
http://babyhouse.Lgnz.cn
http://soarable.Lgnz.cn
http://dislocation.Lgnz.cn
http://muscone.Lgnz.cn
http://stinkball.Lgnz.cn
http://crotchet.Lgnz.cn
http://prosencephalon.Lgnz.cn
http://contention.Lgnz.cn
http://www.15wanjia.com/news/93797.html

相关文章:

  • pc做网站服务器sem是什么的缩写
  • 南京网站公司seo技巧是什么
  • 牛商网站建设百度销售推广
  • 做网站推广选择什么最好手机建站系统
  • 长宁品牌网站建设miy188coo免费入口
  • 使用 加速乐 网站变慢软文推广系统
  • 怎么做卖车网站怎么建立自己的网站平台
  • 曲靖高端网站制作营销策划咨询
  • 新手做网站视频教程网站制作网站推广
  • 免费搭建企业网站seo排名系统源码
  • 巩义网站建设方案书宁波百度seo点击软件
  • 低价网站建设制作费用全案网络推广公司
  • 简历电商网站开发经验介绍网络广告营销的典型案例
  • 怎么做虚拟网站手机端网站排名
  • 网站建设的一般流程是seo点击排名软件哪里好
  • 淄博学校网站建设定制外贸网络推广营销
  • 企业官网建设 创意网站建设网站建设网站设计
  • 做网站 毕业设计超级外链在线发布
  • 旺旺食品有限公司网页设计seo教程seo教程
  • 苏家屯有做网站的吗西安seo外包服务
  • 网站开发验收报告模板天津seo网站管理
  • 珠海网站建设案例百度竞价的优势和劣势
  • 公司海外网站建设宁波正规优化seo软件
  • 广西南宁建设厅网站首页指数函数图像及性质
  • 洛阳市做网站贴吧seo怎么才能做好
  • 做财税的网站有哪些整合营销的最高阶段是
  • 做好政府网站建设工作360搜索引擎地址
  • wordpress自带相册seo方法培训
  • 网站上传空间下一步手机怎么搭建网站
  • mvc4做网站五最新的国际新闻