【问题标题】:Is there a way to add a text (link) to the original data?有没有办法向原始数据添加文本(链接)?
【发布时间】:2017-07-14 19:22:31
【问题描述】:

我正在抓取一些网站。

链接不正确。 页面打不开。

所以我想添加一个指向原始数据的链接

或者也许有比我想象的更好的方法。

如果有好的方法请告诉我

-前-

[[地址错误]]

/qna/detail.nhn?d1id=7&dirId=70111&docId=280474152

[[你要添加的文字]]

我想在我的代码前面添加一个地址(# Bulletin url)

http://~naver.com

library(httr)
library(rvest)
library(stringr)


# Bulletin URL
list.url = 'http://kin.naver.com/qna/list.nhn?m=expertAnswer&dirId=70111'

# Vector to store title and body
titles = c()
contents = c()

#  1 to 10 page bulletin crawling
for(i in 1:10){
  url = modify_url(list.url, query=list(page=i))  # Change the page in the bulletin URL
  h.list = read_html(url, encoding = 'utf-8')  # Get a list of posts, read and save html files from url

  # Post link extraction
  title.link1 = html_nodes(h.list, '.title') #class of title
  title.links = html_nodes(title.link1, 'a') #title.link1 to a로

  article.links = html_attr(title.links, 'href') 
  #Extract attrribute

  for(link in article.links){
    h = read_html(link)  # Get the post

    # title
    title = html_text(html_nodes(h, '.end_question._end_wrap_box h3'))

    title = str_trim(repair_encoding(title))

    titles = c(titles, title)

    # content
    content = html_nodes(h, '.end_question .end_content._endContents')

    ## Mobile question content
    no.content = html_text(html_nodes(content, '.end_ext2'))

    content = repair_encoding(html_text(content))

    ## Mobile question content
    ## ex) http://kin.naver.com/qna/detail.nhn?d1id=8&dirId=8&docId=235904020&qb=7Jes65Oc66aE&enc=utf8&section=kin&rank=19&search_sort=0&spq=1
    if (length(no.content) > 0)
    {
      content = str_replace(content, repair_encoding(no.content), '')
    }

    content <- str_trim(content)

    contents = c(contents, content)

    print(link)

  }
}

# save
result = data.frame(titles, contents)

【问题讨论】:

  • 将 url 转换为字符串将其与您拥有的字符串 (Http: // ~ naver.com) 连接起来。保存输出并将其添加到您想要的列表中。在运行字符串之前这样做会更聪明。如果不添加,您可以检查 url 是否有“http ...”。我会编写代码并发布答案,但我不知道 R...
  • 我知道 R,但我不明白你想做什么。您能否提供预期的输出或其他内容以帮助我理解?
  • @F.Privé Florian.prive.21@gmail.com 我可以在这里发邮件吗? (详细解释,可执行文件...)如果您允许,我会在两个小时内发送邮件。
  • 是的,如果你愿意,但最好是任何人都可以访问,这样任何人都可以帮助你。

标签: r dataframe machine-learning web-scraping web-crawler


【解决方案1】:

如果你在 forloop 之前添加article.links &lt;- paste0("http://kin.naver.com", article.links),这似乎工作(运行)。

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 2015-12-16
    • 2010-11-20
    • 2021-01-16
    • 2015-11-19
    • 1970-01-01
    • 2019-06-21
    • 2011-01-11
    • 1970-01-01
    相关资源
    最近更新 更多