scrapy 如何刮有加载器的网站?

6ovsh4lw  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(179)

我试图刮包含加载屏幕的网站。当我浏览网站时,它显示正在加载..一秒钟,然后它加载了。但问题是,当我试图刮它使用scrapy它给我什么(可能是因为加载的原因)。我可以使用scrapy解决这个问题吗?或者我应该使用其他一些工具吗?在这里'如果你想看https://www.graana.com/project/601/lotus-lake-towers,这是网站的链接

h4cxqtbf

h4cxqtbf1#

x1c 0d1x由于它发送GET请求以获取有关属性的信息,您应该在代码中模仿相同的操作。(您可以在console -〉Network -〉XHR下观察GET调用)


# -*- coding: utf-8 -*-

    import scrapy

    class GranaSpider(scrapy.Spider):
        name = 'grana'
        allowed_domains = 'www.graana.com'
        start_urls = ['https://www.graana.com/api/area/slug/601']

        def parse(self, response):
    #        for url in allurlList:
            scrapy.http.Request(response.url, method='GET' , dont_filter=False)
            print(response.body)

# convert json response to array and save to your storage system

输出是json格式的,转换成你方便的格式。

fumotvh3

fumotvh32#

我知道这个问题是老问题,已经回答过了,但我想分享我的解决方案后,遇到了类似的问题。接受的答案对我没有帮助,因为我没有使用scrapy。

问题

抓取先显示加载页面,然后显示实际内容的网站。
下面是这样一个website的示例:

requests library不会对这样的网站起作用。根据我的经验,request.get(URL, headers=HEADERS)只是超时。

溶液

使用Selenium

  • 首先你需要知道加载页面的动画大约持续多久。在上面的网站中,大约需要3秒。
  • 诀窍是在使用driver.get(URL)导航到网站后,在动画持续时间内使程序休眠。
  • 当程序完成休眠时,加载动画将结束,因此我们可以使用driver.page_source安全地提取实际页面内容的HTML。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

# the following options are only for setup purposes

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)

URL = "https://www.myjob.mu/ShowResults.aspx?Keywords=&Location=&Category=39&Recruiter=Company&SortBy=MostRecent"

driver.get(URL)
time.sleep(5) # any number > 3 should work fine
html = driver.page_source
print(html)

Beautifulsoup库然后可以用于解析HTML。

相关问题