【问题标题】:Chrome preview differs from downloadChrome 预览与下载不同
【发布时间】:2019-08-30 16:51:57
【问题描述】:

我检查以下页面: https://www.dm-jobs.com/Germany/search/?searchby=location&createNewAlert=false&q=&locationsearch=&geolocation=&optionsFacetsDD_customfield4=&optionsFacetsDD_customfield3=&optionsFacetsDD_customfield2=

https://www.dm-jobs.com/Germany/search/?q=&sortColumn=referencedate&sortDirection=desc&searchby=location&d=15

据我了解,数据可以通过 get/post 获取,在“原始”html 源中获取,也可以执行一些 JavaScript 代码。

但是在那个页面上我不知何故找不到源。

Chrome 网络上的数据表明数据(此处为页面上的作业数据)在 Doc(ument) 中 [参见屏幕截图 - Tab Doc],当我查看预览选项卡时,它是空的。但是,如果我查看“响应”选项卡,可以看到数据。

所需的输出:

目标语言是 R,但实际上与这里无关。我很高兴了解数据是如何生成的。所以不需要一些硒方法或类似的方法。但更多的是了解数据是如何生成的,以及如何通过 post/get、JS 或原始源提取数据。

我尝试了什么:

library(httr)
library(rvest)
url <- "https://www.dm-jobs.com/Germany/search/?searchby=location&createNewAlert=false&q=&locationsearch=&geolocation=&optionsFacetsDD_customfield4=&optionsFacetsDD_customfield3=&optionsFacetsDD_customfield2="

src <- read_html(url)
src %>% html_nodes(xpath = "//*[contains(text(), 'Filialmitarbeiter')]")
as.character(src) %>% grep(pattern = "Filialmitarbeiter")

get <- GET(url)
content(get)
content(get$content)

目标输出:

例如

Filialmitarbeiter (w/m/d) 15-30 Std./Wo.    Bad Reichenhall, DE, 83435  30.08.2019  
Filialmitarbeiter (w/m/d) 6-8 Std./Wo.  Neuenburg am Rhein, DE, 79395   30.08.2019  
Führungsnachwuchs Filialleitung (w/m/d) Vechta, DE, 49377   30.08.2019  

【问题讨论】:

  • 什么是示例 iinput 搜索值?
  • 我做了更新。还涉及目标页面。如果我事先没有访问过,它似乎没有显示任何数据。最后添加目标输出。
  • 我仍在展示需要输入值的搜索页面。我需要在搜索框中输入什么值才能获得与您的问题相似的结果?
  • 该页面以某种方式表现得很奇怪。如果您点击直接链接,搜索将不起作用。您可以改为在右上角选择 Deutschland/Germany,然后按“Stellensuchen / 搜索位置”。然后你应该看到 Job 数据。不便之处敬请见谅。
  • 已经非常感谢了。我对背后的过程比刮更感兴趣。很高兴知道这是由于 Cookie。你能分享一下你是怎么发现的吗?作为答案也是完美的,我会赞成并接受。如果我可以另外问:GET(url, set_cookies(...)) 是一种可能的方式吗?

标签: html r api google-chrome rvest


【解决方案1】:

有两个重要的 cookie 必须从初始登录页面中提取。您可以使用html_session 动态捕获这些内容,然后在后续请求中将它们传递给您想要结果的页面(至少对我而言)。我写了一些关于会话对象的东西here

看到的 3 个 cookie 是:

cookies = c(
  'rmk12' = '1',
  'JSESSIONID' = 'some_value',
  'cookie_j2w' = 'some_other_value'
)

在尝试查看职位列表时,您可以使用网络选项卡监控网络流量,从而找到这些以及标题。

您可以尝试删除标头和 cookie,您会发现只需要第二个和第三个 cookie 而不需要标头。但是,必须在对 url 的先前请求中捕获传递的 cookie,如下所示。 Session 是执行此操作的传统方式。


R

library(rvest)
library(magrittr)

start_link = 'https://www.dm-jobs.com/Germany/?locale=de_DE'
next_link <- 'https://www.dm-jobs.com/Germany/search/?searchby=location&createNewAlert=false&q=&locationsearch=&geolocation=&optionsFacetsDD_customfield4=&optionsFacetsDD_customfield3=&optionsFacetsDD_customfield2='
jobs <- html_session(start_link) %>% 
        jump_to(.,next_link) %>% 
        html_nodes('.jobTitle-link') %>% 
        html_text()
print(jobs)

import requests
from bs4 import BeautifulSoup as bs

with requests.Session() as s:
    r = s.get('https://www.dm-jobs.com/Germany/?locale=de_DE')
    cookies = s.cookies.get_dict() # just to demo which cookies are captured
    print(cookies) # just to demo which cookies are captured
    r = s.get('https://www.dm-jobs.com/Germany/search/?searchby=location&createNewAlert=false&q=&locationsearch=&geolocation=&optionsFacetsDD_customfield4=&optionsFacetsDD_customfield3=&optionsFacetsDD_customfield2=')
    soup = bs(r.content, 'lxml')
    print(len(soup.select('.jobTitle-link')))

阅读:

  1. html_session

【讨论】:

    猜你喜欢
    • 2014-11-18
    • 2014-11-25
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2015-06-03
    • 2013-05-08
    • 2017-01-14
    相关资源
    最近更新 更多