抓取网页并需要选择正确的选择器
Scraping a web page and need to pick right selector
这是我在看了几个教程后第一次使用 Scrapy,我正在尝试抓取这个 url
https://www.hackster.io/arduino/members
我想为每个用户配置文件获取 links。我运行我的scrapyshell如下
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())
但我只得到 [] 作为输出
我想得到附件照片中指定的link,谁能看看我的命令是否有问题并告诉我?
url to be scraped
当我使用 google 的 chorme 检查选项并立即复制选择器时,我得到了相同的输出
#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
那是因为您在 Chrome 控制台中看到的 html 是在 javascript 中构建的客户端。默认情况下,Scrapy 不会解释 javascript 并读取服务器发送的页面源代码。请参阅我的回答 here 以找到解决您问题的方法。
检查 scrapy 爬虫得到的响应:-
- 打开终端
- 运行 命令 scrapy shell https://www.hackster.io/arduino/members
- 运行命令查看(响应)
抓取工具看到的响应将显示在您的默认网络浏览器中。
从这个响应中,您可以检查您的抓取工具是否正在获取您想要抓取的内容!
正如我从响应中看到的,您没有在响应中得到 Arduino_Genuino,这绝对是客户端 javascript 呈现的情况。
Screenshot of the webpage as visible to the crawler.
要从此类页面中抓取数据,您需要使用 javascript 渲染引擎,例如在您的 localhost:8050[= 上运行的 scrapy-splash 13=]
您必须传递 url 才能抓取到启动画面渲染引擎,并且在 javascript 完全加载到 localhost:8050 处的启动画面中一段时间后,您必须抓取来自那里的数据。
这是我在看了几个教程后第一次使用 Scrapy,我正在尝试抓取这个 url
https://www.hackster.io/arduino/members
我想为每个用户配置文件获取 links。我运行我的scrapyshell如下
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())
但我只得到 [] 作为输出
我想得到附件照片中指定的link,谁能看看我的命令是否有问题并告诉我?
url to be scraped
当我使用 google 的 chorme 检查选项并立即复制选择器时,我得到了相同的输出
#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
那是因为您在 Chrome 控制台中看到的 html 是在 javascript 中构建的客户端。默认情况下,Scrapy 不会解释 javascript 并读取服务器发送的页面源代码。请参阅我的回答 here 以找到解决您问题的方法。
检查 scrapy 爬虫得到的响应:-
- 打开终端
- 运行 命令 scrapy shell https://www.hackster.io/arduino/members
- 运行命令查看(响应)
抓取工具看到的响应将显示在您的默认网络浏览器中。
从这个响应中,您可以检查您的抓取工具是否正在获取您想要抓取的内容!
正如我从响应中看到的,您没有在响应中得到 Arduino_Genuino,这绝对是客户端 javascript 呈现的情况。
Screenshot of the webpage as visible to the crawler.
要从此类页面中抓取数据,您需要使用 javascript 渲染引擎,例如在您的 localhost:8050[= 上运行的 scrapy-splash 13=]
您必须传递 url 才能抓取到启动画面渲染引擎,并且在 javascript 完全加载到 localhost:8050 处的启动画面中一段时间后,您必须抓取来自那里的数据。