无法想出如何“抓取”一个带有Scrapy类名的<a>标记

2024-10-01 22:27:36 发布

您现在位置:Python中文网/ 问答频道 /正文

为了一个研究项目,我在浏览一些中国网站,遇到了一个我无法破解的问题。使用scrapy,我试图跟踪类名为“up”的标记中的链接(该标记是导航栏中许多标记之一)。问题是CSS或Xpath选择器都无法“抓取”标记。我可以很好地获取它的父div标记,但是当我试图访问它的子对象时,scrapy返回一个空列表。下面是父div及其子标记的结构:

    <div class="xz-page clearfix" style="margin-left:160px;">
        <script type="text/javascript"> ... </script>
        <a class="pre" href="javascript:void(0)" style="width:40px">上一页</a>
        <a class="num sel" href="javascript:void(0)">1</a>
        <a class="num" href="index_1.html">2</a>
        <a class="num" href="index_2.html">3</a>
        <a href="javascript:void(0)" style="border:none">...</a>
        <a class="num" href="index_697.html">698</a>
        <a class="num" href="index_698.html">699</a>
        <a class="up" href="index_1.html" style="width:40px">下一页</a>&nbsp;
    </div>

我想抓住的链接是最后一个。在

当我使用选择器时:

^{pr2}$

它返回这个:

[<Selector xpath='//div[@class = "xz-page clearfix"]' data='<div class="xz-pageclearfix" style="mar'>]

但当我试图用这个来访问标签时:

next_page = response.xpath('//div[@class = "xz-page clearfix"]/a[@class ="up"]')

或者只是为了抓住所有的孩子:

next_page = response.xpath('//div[@class = "xz-page clearfix"]/a')

它返回一个空列表[]

我尝试了多次以上选择器的迭代,每次都得到一个空列表。在

即使只是想用这个做标记:

next_page = response.xpath('//a[@class = "up"]')

返回空列表

我怀疑这和脚本标签有关。在

脚本标记中的脚本如下所示:

<script type="text/javascript">
function createPageHTML(_nPageCount, _nCurrIndex, _sPageName, _sPageExt){
  if(_nPageCount == null || _nPageCount<=1){
    return;
  }

  var nCurrIndex = _nCurrIndex || 0;
  // 1 输出首页和上一页
  // 1.1 当前页是首页
  if(nCurrIndex == 0){
    document.write("<a class=\"pre\" href=\"javascript:void(0)\" style=\"width:40px\">上一页</a>");
    document.write("<a class=\"num sel\" href=\"javascript:void(0)\">1</a>");
  }
  //1.2 当前页不是首页
  else{
    var nPreIndex = nCurrIndex - 1;
    var sPreFileExt = nPreIndex == 0 ? "" : ("_" + nPreIndex);

    document.write("<a class=\"next\" href=\"" + _sPageName + sPreFileExt + "."+_sPageExt+"\" style=\"width:40px\">上一页</a>");
    document.write("<a class=\"num\" href=\""+_sPageName+"."+_sPageExt+"\">1</a>");
  }

  // 2 输出中间分页
  var flag1=true;
  var flag2=true;

  for(var i=1; i<_nPageCount; i++){

    if(i-nCurrIndex<3&&nCurrIndex-i<3||i<2||_nPageCount-i<3){
      if(nCurrIndex == i)
        document.write("<a class=\"num sel\" href=\"javascript:void(0)\">"+(i+1)+"</a>");
      else
        document.write("<a class=\"num\" href=\""+_sPageName+"_" + i + "."+_sPageExt+"\">"+(i+1)+"</a>");
    }
    else if(i-nCurrIndex>3&&flag1){
      flag1=false;
      document.write("<a href=\"javascript:void(0)\" style=\"border:none\">...</a>");
    }
    else if(nCurrIndex-i>3&&flag2){
      flag2=false;
      document.write("<a href=\"javascript:void(0)\" style=\"border:none\">...</a>");
    }
  }

  // 3 输出下一页和尾页
  // 3.1 当前页是尾页
  if(nCurrIndex == (_nPageCount-1)){
    document.write("<a  class=\"up\" href=\"javascript:void(0)\"  style=\"width:40px\">下一页</a>&nbsp;");
    //document.write("<li><a href=\""+_sPageName+"_" + (_nPageCount-1) + "."+_sPageExt+"\">尾页</a></li>");
  }
  // 3.2 当前页不是尾页
  else{
    var nNextIndex = nCurrIndex + 1;
    var sPreFileExt = nPreIndex == 0 ? "" : ("_" + nPreIndex);
    document.write("<a  class=\"up\" href=\""+_sPageName+"_" + nNextIndex + "."+_sPageExt+"\"  style=\"width:40px\">下一页</a>&nbsp;");
    //document.write("<li><a href=\""+_sPageName+"_" + (_nPageCount-1) + "."+_sPageExt+"\">尾页</a></li>");
  }

}

createPageHTML(699, 0, "index", "html");
</script>

我不知道JS,所以我不太确定这段代码在做什么。 我访问页面的其他部分没有问题,只有这一个标签给我带来了问题。这只是我遇到麻烦的一个中文网站,然而,我正在抓取的英文来源没有给我任何问题。我不确定问题是因为我在网络抓取方面缺乏经验,还是在中国网站上发生了一些奇怪的事情。在

{1}是为了参考网页。在底部导航到下一页的按钮是给我带来麻烦的。在

任何建议或帮助将不胜感激。谢谢!在


Tags: 标记divstylevarpagejavascriptdocumentnum

热门问题