【问题标题】:GET {httr} returns a Bad Request responseGET {httr} 返回错误请求响应
【发布时间】:2014-12-01 23:51:26
【问题描述】:

我正在尝试抓取存储在searchlink 中的 url 的 html 元素。对我有用的唯一方法是htmlTreeParse {XML}。但是它没有返回我正在寻找的元素。例如:img[@title='Add to compare']

searchlink <- "http://www.realtor.ca/Map.aspx#CultureId=1&ApplicationId=1&RecordsPerPage=9&MaximumResults=9&PropertyTypeId=300&TransactionTypeId=2&SortOrder=A&SortBy=1&LongitudeMin=-114.52066040039104&LongitudeMax=-113.60536193847697&LatitudeMin=50.94776904194829&LatitudeMax=51.14246522072541&PriceMin=0&PriceMax=0&BedRange=0-0&BathRange=0-0&ParkingSpaceRange=0-0&viewState=m&Longitude=-114.063011169434&Latitude=51.0452194213867&ZoomLevel=11&CurrentPage=1" 

doc <- htmlTreeParse(searchlink,useInternalNodes = T)


   classes <- xpathSApply(doc,"//img[@title='Add to compare']",function(x){xmlGetAttr(x,'class')})

上面运行类的结果:

list()

我也尝试过readLinesGET {httr},但它们在读取 url 时都返回错误。我猜这是因为 url 中的特殊字符,但不知道如何修复它。回复如下:

Response [http://www.realtor.ca/Map.aspx#CultureId=1&ApplicationId=1&RecordsPerPage=9&MaximumResults=9&PropertyTypeId=300&TransactionTypeId=2&SortOrder=A&SortBy=1&LongitudeMin=-114.52066040039104&LongitudeMax=-113.60536193847697&LatitudeMin=50.94776904194829&LatitudeMax=51.14246522072541&PriceMin=0&PriceMax=0&BedRange=0-0&BathRange=0-0&ParkingSpaceRange=0-0&viewState=m&Longitude=-114.063011169434&Latitude=51.0452194213867&ZoomLevel=11&CurrentPage=1]
  Date: 2014-12-01 16:46
  Status: 400
  Content-type: text/html; charset=us-ascii
  Size: 324 B
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid URL</h2>
<hr><p>HTTP Error 400. The request URL is invalid.</p>
</BODY></HTML> 

【问题讨论】:

    标签: r httr


    【解决方案1】:

    尝试删除网址中的#,我只是替换为?

    library("httr")
    url <- "http://www.realtor.ca/Map.aspx?CultureId=1&ApplicationId=1&RecordsPerPage=9&MaximumResults=9&PropertyTypeId=300&TransactionTypeId=2&SortOrder=A&SortBy=1&LongitudeMin=-114.52066040039104&LongitudeMax=-113.60536193847697&LatitudeMin=50.94776904194829&LatitudeMax=51.14246522072541&PriceMin=0&PriceMax=0&BedRange=0-0&BathRange=0-0&ParkingSpaceRange=0-0&viewState=m&Longitude=-114.063011169434&Latitude=51.0452194213867&ZoomLevel=11&CurrentPage=1"
    res <- GET(url)
    tt <- content(res)
    

    然后解析tt中的html内容

    【讨论】:

    • 感谢 Scott 成功了!!但我仍然无法提取我需要的 html 元素。我正在尝试解析该网页上所有“添加到比较”按钮的属性。我试过xpathSApply(tt,"//img[@title='Add to compare']",function(x){xmlGetAttr(x,'class')}),但它返回NULL。我不确定问题出在哪里。有什么想法吗?
    • 如果你这样做 xpathSApply(tt, "//img[@title]", xmlGetAttr, name="title") 我看不到任何标题带有 Add to compare 属性的标题
    • 这就是我觉得奇怪的原因,因为如果您检查网页上的元素,您会看到以下内容:&lt;img id="com_img_15023510" class="compareIcon com-15023510" src="/Presentation/Images/common/icons/compare_unselected.png" alt="Add to compare" title="Add to compare"&gt;。所以由于某些原因,在阅读 html 时,似乎整个 html 代码都没有被抓取。
    • 您可能想尝试RSelenium cran.r-project.org/web/packages/RSelenium/index.html,它可以在 R 中自动执行 Web 浏览器操作,SO 中有很多示例问题
    • 我感觉元素没有被拉出的原因是因为它们位于侧面板中,因此位于不同的框架中。我会尝试switchToFrame{RSelenium} 看看它是否会起作用。感谢您的提示!
    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多