Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何修复webscraping中的HTTP 403错误?_Html_Css_R_Xml - Fatal编程技术网

Html 如何修复webscraping中的HTTP 403错误?

Html 如何修复webscraping中的HTTP 403错误?,html,css,r,xml,Html,Css,R,Xml,我正试图收集3600多个维基百科页面上的统计数据。我正在尝试使用R中的web抓取实现自动化 我有一个问题,直接在R中提取HTML代码 download_html("xtools.wmflabs.org/articleinfo/fr.wikipedia.org/1re_Convention_nationale_acadienne") 这是控制台告诉我的: download_html("xtools.wmflabs.org/articleinfo/fr.wikiped

我正试图收集3600多个维基百科页面上的统计数据。我正在尝试使用R中的web抓取实现自动化

我有一个问题,直接在R中提取HTML代码

download_html("xtools.wmflabs.org/articleinfo/fr.wikipedia.org/1re_Convention_nationale_acadienne")
这是控制台告诉我的:

download_html("xtools.wmflabs.org/articleinfo/fr.wikipedia.org/1re_Convention_nationale_acadienne")
Error in curl::curl_download(url, file, quiet = quiet, mode = mode, handle = handle) : HTTP error 403.
这不起作用的可能原因是什么

当我将HTML保存为文件并在R中运行时,一切都很完美,我可以用结果创建一个数据框:

# ID webpage link first
setwd("C:\\Users\\judit\\Scraping dans R")
webpage <- read_html("HTML_1e.html")
# read_html("https://xtools.wmflabs.org/articleinfo/fr.wikipedia.org/1re_Convention_nationale_acadienne?uselang=fr")


# Statistiques: extraction ----

# Stats: titre
titre <- html_nodes(webpage, ".back-to-search+ a")
titre <- html_text(titre, trim=TRUE)

# Stats: Taille de page
taille <- html_nodes(webpage, ".col-lg-5 tr:nth-child(3) td+ td")
taille <- html_text(taille, trim=TRUE)

# Stats: Total des modifications
mod <- html_nodes(webpage, ".col-lg-5 tr:nth-child(4) td+ td")
mod <- html_text(mod, trim=TRUE)

# Stats: Nombre de redacteurs
red <- html_nodes(webpage, ".col-lg-5 tr:nth-child(5) td+ td")
red <- html_text(red)

# Stats: Evaluation
evaluation <- html_nodes(webpage, ".col-lg-5 tr:nth-child(6) td+ td")
evaluation <- html_text(evaluation, trim=TRUE)

# Stats: Liens vers cette page
liens_vers <- html_nodes(webpage, ".stat-list--group tr:nth-child(2) a")
liens_vers <- html_text(liens_vers, trim=TRUE)

# Stats: Liens depuis cette page
liens_depuis <- html_nodes(webpage, ".col-lg-offset-1 .stat-list--group tr:nth-child(4) td+ td")
liens_depuis <- html_text(liens_depuis, trim=TRUE)

# Stats: Mots
mots <- html_nodes(webpage, ".col-lg-3 tr:nth-child(3) td+ td")
mots <- html_text(mots, trim=TRUE)

wikipedia <- data.frame(titre, taille, red, mod, evaluation, liens_vers, liens_depuis)
#首先是ID网页链接
setwd(“C:\\Users\\judit\\Scraping dans R”)

网页对于那些也在研究从维基百科页面中提取数据的人,我发现一些软件包可以帮助我通过直接从R获取数据来规避403问题

我使用以下软件包:

  • 维基百科
  • 维基百科
这是我收集文章基本信息的代码:

# Basic information ----

library("WikipediR")

pageinfo <- page_info(language = "fr", 
                      project = "wikipedia", 
                      page = "1re Convention nationale acadienne",
                      properties = c("url"),
                      clean_response = T)

pageinfo_df1=data.frame(pageinfo)
pageinfo_df2=data.frame(id=pageinfo_df1$pageid, title=pageinfo_df1$title, lang=pageinfo_df1$pagelanguage, sizeBytes=pageinfo_df1$length, url=pageinfo_df1$fullurl)
#基本信息----
图书馆(“维基百科”)

pageinfo我以前在某个网站上遇到过这种情况,我的解释(完全没有受过教育)是该网站认定我是黑客并阻止了我。我能够在几分钟后运行相同的代码,但要么从未收到403错误,要么在不同的URL上收到错误,我决定不再进一步排除故障。是的,确切地说,当我手动完成工作时,在打开足够多的页面后,它会要求我登录,因为它认为我是机器人。也许我需要找到一种通过R登录的方法?如果您试图在短时间内快速下载大量文件,网站可能会有一个关于每秒页面请求数的报价。使用
Sys.sleep()
函数在每个页面请求之间添加延迟。如果您使用0.5到1秒的延迟(可能稍小),它应该可以防止引用冲突。@Dave2e,这听起来是解决这个问题的一个很好的通用解决方案。你愿意把它写下来作为回答吗?几个月前,这个答案对我正在编写的一些代码非常有用:-)@Dave2e谢谢你的提醒!我还没有开始一次在多个页面上运行代码,但我相信当我开始运行时,这会很有用。
# Links from this article
links_page <- links(page = "1re Convention nationale acadienne", domain = "fr")
links_page_df <- as.data.frame(links_page$links)
nrow(links_page_df)