PAT-使用爬虫获取个人排名

直接看代码吧,不懂的可以留言
使用时更改自己rank的大致页码,然后修改用户名即可

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
url='https://www.patest.cn/contests/pat-a-practise/ranklist?page='
s = requests.session()
for i in range(100,150):
    print 'We are now at '+str(i)+' please wait some seconds...'
    new_url=url+str(i)
    response = s.get(new_url)
    soup = BeautifulSoup(response.content,"html.parser")
    trs = soup.find_all('tr')
    for tr in trs:
        flag = 0
        for td in tr:
            if td.string=='god_speed':
                print 'We find it!!!'
                print 'your page is '+str(i)
                # print 'here is the result\n'+str(tr)
                tds = tr.find_all('td')
                name = tds[1].find_all('a')
                print 'rank= ' + str(tds[0].contents[0])
                print 'username is ' + str(name[0].contents[0])
                print 'personal description is ' +str(tds[2].contents[0])
                print 'pass ' + str(tds[3].contents[0])
                print 'score ' + str(tds[4].contents[0])
                flag = 1
                break
        if flag:
            break
    if flag:
        break

你可能感兴趣的:(爬虫)