代码之家  ›  专栏  ›  技术社区  ›  Sayeed

网页抓取:缺少某些网页的URL

  •  0
  • Sayeed  · 技术社区  · 7 年前

    我试图删除网站的前9页,但第5页和第7页似乎不见了。这使show python成为一个属性错误。不过,我认为“if”函数可以解决这个问题,但我无法找出if函数的代码。 这是我的密码

    import requests
    from bs4 import BeautifulSoup
    base_url="http://cbcs.fastvturesults.com/student/1sp15me00"
    for page in range(1,10,1):
        r=requests.get(base_url+str(page))
        c=r.content
        soup=BeautifulSoup(c,"html.parser")
        items=soup.find(class_="text-muted")
        if ??????????:
            pass
        else:
            print("{}\n{}".format(items.previous_sibling,items.text))
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   SIM    7 年前

    你不需要创造 else 封锁这里。只检查 if items is not None 够了。尝试以下方法:

    items = soup.find(class_="text-muted")
    if items:
        print("{}\n{}".format(items.previous_sibling,items.text))
    
        2
  •  2
  •   nmog    7 年前

    当您尝试访问 items 什么时候 项目 设置为 None . 当美女组找不到 class_="text-muted"

    解决方案:

    if not items:
        continue
    

    请注意 pass (从您的解决方案中)将只传递当前语句并转到循环中的下一行。 continue 将结束当前迭代并继续下一个迭代。