2017-08-01 70 views
0

我目前正在試圖用scrapy刮取MSN新聞,並且在scrapy外殼內獲取瀏覽器的正確響應時遇到了一些困難。用Scrapy刮掉MSN新聞

當我去https://www.msn.com/en-us/news/world在瀏覽器中,我看到:Screenshot of what the page is supposed to look like 這是完美的,因爲這是該頁面應該是什麼樣子,但是當我運行命令scrapy shell https://www.msn.com/en-us/news/world,然後view(response)這是我所看到的來代替。 Screenshot of incorrect response

我試過禁用JavaScript來查看是否可能內容正在加載ajax,這就是爲什麼它不工作,但所做的只是停止加載縮略圖。有人知道爲什麼這樣做?

回答

0

該網站肯定有很多JavaScript運行。你應該如何處理這個問題,就是在一個實例中禁用javascript,並且在一側擁有一個普通實例。
然後,你可以挖掘和比較,即找到縮略圖ID和搜索它沒有JavaScript的來源 - 它可能是在某個地方的JSON或JavaScript變量。

This is what scrapy sees已禁用JavaScript。
您可以看到文章名稱和簡短描述。如果你檢查標題,你甚至可以看到有一個縮略圖的鏈接!

articles = response.xpath("//li[@data-m]/a[@aria-label]") 
for article in articles: 
    # thumbnail 
    response.xpath('img/@data-src').extract_first() 
    # '{"default":"//img-s-msn-com.akamaized.net/tenant/amp/entityid/AAp0iW6.img?h=414&w=624&m=6&q=60&u=t&o=t&l=f&f=jpg&x=1280&y=688"}' 
    # title 
    article.xpath("@aria-label").extract_first() 
    # 'north korea can hit most of united states: u.s. officials provided by reuters' 
    # description 
    article.xpath("/img/@alt").extract_first() 
    # This Friday, July 28, 2017, photo distributed by the Nort... 
+0

我不知道如何禁用javascript得到你在該圖片中顯示的結果,當我通過鉻禁用javascript這是我看到:[鏈接](https://imagebin.ca/v/ 3VOUoErN9dgo) – Jason

+0

@Jason你有沒有試過我的代碼? – Granitosaurus

+0

是的,'articles'只是一個空白的數組,scrapy仍然只能得到我在問題 – Jason