[英]Getting an empty response from scrapy shell using xpath, while it works in element inspector
[英]Getting empty response from scrapy shell while crawling monsterindia.com
我正在嘗試從monsterindia.com抓取一些頁面。 但是,每當我在scrapy shell上編寫任何xpath時,它都會給我空的結果。 但是,應該有某種方法,因為view(response)命令為我提供了相同的html頁面。
我運行了以下命令:
scrapy shell "https://www.monsterindia.com/search/computer-jobs"
在我的終端上,然后嘗試了幾種方法來制定不同的xpath,例如-response.xpath response.xpath('//*[@class="job-tittle"]/text()').extract()
。 但是,沒有運氣..總有空結果。
在終端上:
scrapy shell "https://www.monsterindia.com/search/computer-jobs"
然后, response.xpath('//div[@class="job-tittle"]/text()').extract()
得到空結果。
然后, response.xpath('//*[@class="card-apply-content"]/text()').extract()
得到空結果。
您要查找的數據不在主頁上,而是在頁面加載后檢索到的響應中。 如果您在瀏覽器中選中“ 查看頁面源 ”,則將看到第一個請求中實際包含的內容。
通過檢查開發工具中的“網絡”標簽,您將看到其他請求,例如對此URL的請求: https : //www.monsterindia.com/middleware/jobsearch?query=computer&sort=1&limit=25
因此,我認為Thiago遇到的問題是該頁面使用xhr請求進行更新,其中包括結果計數查詢字符串參數。 這將返回您可以解析的json。 因此,您將網址更改為該網址並相應地處理json。
使用請求進行演示
import requests
from bs4 import BeautifulSoup as bs
import json
r = requests.get('https://www.monsterindia.com/middleware/jobsearch?query=computer&sort=1&limit=100')
soup = bs(r.content, 'lxml')
data = json.loads(soup.select_one('p').text)['jobSearchResponse']['data']
for item in data:
print(item)
第一項的JSON
https://jsoneditoronline.org/?id=fe49c53efe10423a8d49f9b5bdf4eb36
帶有刮擦:
jsonres = json.loads(response.body_as_unicode()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.