刮一个网页,需要选择正确的

2024-10-01 22:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次使用Scrapy在看了几篇辅导之后,我正在尝试刮这个网址

https://www.hackster.io/arduino/members

我想得到每个用户配置文件的链接。我把我的脏壳做了如下检查

print(response.css("#main > div > div > div > div:nth-child(2) > div.hckui__layout__container > div.hckui__layout__wrapper1170 hckui__layout__fullScreenHeight > div > div.common-overlay__parent__1A_nT > div.grid__gridBasic__fjt5B grid__grid__1QeD6 grid__guttersH__2MYvz grid__guttersV__3M28R > div:nth-child(1) > div.undefined hckui__layout__flexCenterItems > div.user_card__content__1YVc5 > a.hckui__typography__bodyM hckui__typography__link hckui__typography__bold::attr(href)").extract())

但我只得到[]作为输出

你知道吗

我想得到所附照片中指定的链接,有人能看一下并告诉我我的命令是否有问题吗?你知道吗

url to be scraped

当我使用google的chorme inspect选项并立即复制选择器时,我得到了相同的输出

#main > div > div > div > div:nth-child(2) > div > div > div > div.common-overlay__parent__1A_nT > div > div:nth-child(1) > div > div > a
or even using

#main > div > div > div > div:nth-child(2) > div > div > div > div.common-overlay__parent__1A_nT > div 

Tags: divchild链接maincommongridparentscrapy
2条回答

要检查scrapy爬虫得到的响应:

  1. 开放式终端
  2. 运行命令scrapy shell https://www.hackster.io/arduino/members
  3. 运行命令查看(响应)

对爬虫程序的响应将显示在默认的web浏览器中。你知道吗

从这个响应中,你可以检查你的爬虫是否得到了你想要抓取的内容!你知道吗

正如我从响应中看到的,您在响应中没有得到Arduinoúu Genino,这绝对是客户端javascript呈现的一个例子。你知道吗

Screenshot of the webpage as visible to the crawler.

要从这些页面中提取数据,您需要使用javascript呈现引擎,例如在您的页面上运行的scrapy splash本地主机:8050你知道吗

必须将url传递给splash呈现引擎,并在javascript完全加载到splash时经过一段时间本地主机:8050,你必须从那里抓取数据。你知道吗

参考启动文档:https://splash.readthedocs.io/en/stable/api.html

这是因为您在Chrome控制台中看到的html是用javascript在客户端构建的。默认情况下,Scrapy不会在服务器发送页面源代码时解释javascript和读取页面源代码。请看我的答案here为您的问题找到解决方案。你知道吗

相关问题 更多 >

    热门问题