Href在scrapy结果中不可见,但在html中可见

LucSpan

设定

我有此页面中的下一页按钮元素

<li class="Pagination-item Pagination-item--next  Pagination-item--nextSolo ">
                        <button type="button" class="Pagination-link js-veza-stranica kist-FauxAnchor" data-page="2" data-href="https://www.njuskalo.hr/prodaja-kuca?page=2" role="link">Sljedeća&nbsp;<span aria-hidden="true" role="presentation">»</span></button>
                    </li>

我需要获取data-href属性中的网址


使用以下简单的xpath到scrapy shell中的button元素,

response.xpath('//*[@id="form_browse_detailed_search"]/div/div[1]/div[5]/div[1]/nav/ul/li[8]/button').extract_first()                        

我检索

'<button type="button" class="Pagination-link js-veza-stranica" data-page="2">Sljedeća\xa0<span aria-hidden="true" role="presentation">»</span></button>'

data-href属性去哪儿了?

如何获取网址?

瓦尔迪尔·史丹姆

data-href属性很可能是由浏览器中运行的某些JavaScript代码计算得出的。如果查看此页面的原始源代码(浏览器中的“查看源代码”选项),则不会在该位置找到该属性。

在开发人员工具上看到的输出是浏览器呈现的DOM,因此您可以期望浏览器视图与Scrapy实际获取的内容(原始HTML源代码)之间存在差异。请记住,Scrapy不执行任何JavaScript代码。

无论如何,解决此问题的一种方法是根据data-page属性构建分页URL

from w3lib.url import add_or_replace_parameter
...

next_page = response.css('.Pagination-item--nextSolo button::attr(data-page)').get()
next_page_url = add_or_replace_parameter(response.url, 'page', next_page)

w3lib是一个开源库:https//github.com/scrapy/w3lib

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

图片在搜索结果中不可见

前景在真实设备中不可见,但在模拟器中可见

HTML视频在SVG中的foreignObject内部不可见

视图在设备中不可见,但在XML中可见

ImageView存在,但在显示中不可见

Vue组件在HTML中不可见

OnClick事件触发但在.aspx中不可见

如何在TextInputLayout Android中更改setEndIconDrawable的可见性(可见,不可见或不可见)?

来自后端的数据在HTML页面中不可见

为什么Bootstrap轮播字幕在开发中可见但在生产中不可见?

发布了android应用,但在Google Play中不可见

表格中的不可见/可见提交按钮

Scala中继承的类型别名在构造函数声明中不可见,但在构造函数主体中可见

文本选择在alert()中可见,但在html()中不可见

字符在vi中可见,但在cat中不可见。

尽管测试结果在运行,但在QUnit中不可见

为什么图像在Firefox中可见但在Chrome中不可见?

Inkscape包装文本框在HTML中不可见

Joomla模块在文章中可见,但在类别中不可见

SVG外部对象中的HTML不可见

UFT 结果在 Jenkins 报告中不可见

包含在 html 中后图像不可见

xcode 崩溃上传成功但在 crashlytics 中不可见

HTML 元素在 WordPress 中不可见

MySQL 数据在 Toad Edge 中可见,但在 CLI 中不可见

Vue 组件在 <template> html 标签中不可见

子组件 HTML 在 VueJS 中不可见

使控件在 JavaScript 中可见,但在网页上不可见

为什么提交在 github 上可见,但在本地代表中不可见?