【问题标题】:R: Scraping the publish date of an specific website using RVestR:使用 RVest 抓取特定网站的发布日期
【发布时间】:2019-10-12 09:08:00
【问题描述】:

我目前正在使用 RVest 在 R 中进行网络抓取。 我目前的网站是“https://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Rheinland-Pfalz/Koblenz”。 但我无法获取房地产报价的各个 pushlishDates。

F.e,这是我发现嵌入在脚本中的 HTML 部分。

"@publishDate":"2019-10-12T10:50:57.831+02:00"

我找不到合适的 RVest-Selector... 下面的代码返回 Character[0],因为节点“publishDate”不起作用。

抓取每个发布日期所需的 html_node() 是什么?提前非常感谢。

library(rvest)

immo_webp <- read_html ("https://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Rheinland-Pfalz/Koblenz")

PDate <- immo_webp %>%
  html_nodes("publishDate") %>%
  html_text()


【问题讨论】:

    标签: r web-scraping rvest


    【解决方案1】:

    内容是从script 标记动态加载的。您可以正则表达式输出适当的字符串并使用jsonlite 进行解析,然后提取到感兴趣的数据框项目中,例如purrr


    R:

    library(rvest)
    library(stringr)
    library(jsonlite)
    library(purrr)
    
    p <- read_html('https://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Rheinland-Pfalz/Koblenz') %>% html_text()
    data <- jsonlite::parse_json(str_match_all(p,'resultListModel: (.*\\})')[[1]][,2])
    results <- data$searchResponseModel$resultlist.resultlist$resultlistEntries[[1]]$resultlistEntry
    
    df <- map_df(results, function(item) {
    
      data.frame(property = item$resultlist.realEstate$address$description$text,
                 datePublished = item$`@publishDate`,
                 stringsAsFactors=FALSE)
    })
    
    View(df)
    


    派:

    from bs4 import BeautifulSoup as bs
    import requests, re, json
    import pandas as pd
    
    r = requests.get('https://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Rheinland-Pfalz/Koblenz')
    p = re.compile(r'resultListModel: (.*})')
    data = json.loads(p.findall(r.text)[0])
    info = [
            (entry['resultlist.realEstate']['address']['description']['text']
            ,entry['@publishDate']) 
            for entry in data['searchResponseModel']['resultlist.resultlist']['resultlistEntries'][0]['resultlistEntry']
            ]
    print(pd.DataFrame(info,columns = ['Property','PublishedDate']))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多