在爬行时从废壳获取空响应印度怪兽网

2024-09-30 22:14:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我从几页纸上爬下来印度怪兽网. 但每当我在scrapy shell上编写xpath时,它都会得到空结果。但是,应该有某种方法,因为view(response)命令提供了相同的html页面。在

我运行了这个命令:

scrapy shell "https://www.monsterindia.com/search/computer-jobs"

在我的终端上,然后尝试了几种不同的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() 得到空结果。在

我希望它能给出一些结果,我是说从网站上爬出来的文字。请帮我拿一下。enter image description here


Tags: texthttps命令comsearchresponsewwwjobs
2条回答

您要查找的数据不在主页上,而是在页面加载后检索到的响应中。如果您在浏览器中选中“viewpagesource”,您将看到第一个请求中实际出现的内容。在

通过检查devtools中的network选项卡,您将看到更多的请求,比如对这个URL的请求:https://www.monsterindia.com/middleware/jobsearch?query=computer&sort=1&limit=25

因此,Thiago想得到的是页面更新时使用了xhr请求,其中包括一个results count查询字符串参数。这将返回您可以解析的json。因此,您将url更改为该值并相应地处理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


用刮痧:

^{pr2}$

相关问题 更多 >