![](/img/trans.png)
[英]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.