python爬取静态页面

今天在微信群看到一个话题,咸鱼如何翻身,我想了想我的翻身机会,貌似只有一发命中注定的红蓝球才能提升生活档次,几年前红蓝球可以网购的时候守了一个号三年,想继续了,想查查自己守过的号有没有在近几年开过,但是发现开奖网站都没提供查询功能,于是决定自己爬一爬。


image.png

爬取下来的效果如下,存到本地文件:


image.png

代码实现:

#python爬取shuangse球历史开奖记录
import requests
import re
#获取所有期号
def select_list():
    '''获取所有的期号列表'''
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'
    }
    url= 'http://kaijiang.500.com/shtml/ssq/19074.shtml'
    page = requests.get(url,headers=headers)
    code = page.content
    code = code.decode('gb2312','ignore')
    # print(code)

    selectRegex = re.compile(r'http://kaijiang.500.com/shtml/ssq/(\d+).shtml')
    select_list = selectRegex.findall(code)
    return select_list


#获取开奖结果,参数:list 一个 期号列表 参数
def awards_ret(awards_list):
    '''获取award_list中所有期号的开奖结果,存入字典'''
    #将开奖期号-开奖结果存入到字典中
    awards_dic = {

    }
    for lst in awards_list:
        url = 'http://kaijiang.500.com/shtml/ssq/'+str(lst)+'.shtml'
        page = requests.get(url)
        code = page.content
        code = code.decode('gb2312','ignore')
        redballRegex = re.compile(r'
  • (\d+)') blueballRegex = re.compile(r'
  • (\d+)') redball = redballRegex.findall(code) blueball = blueballRegex.findall(code) # number = str(redball)+str(blueball) awards_dic[lst] = {'redball':redball,'blueball':blueball} return awards_dic select_list = select_list() awards_dic = awards_ret(select_list) #后面继续爬取可以在下一步自定义期号,免得重复爬大量的历史记录 # awards_dic = awards_ret([19074,19073,19072]) for key,value in awards_dic.items(): res = "开奖期号:" + str(key) + str(value) with open('开奖结果.txt','a',encoding='utf-8') as file_obj: file_obj.write(res+'\n')
  • 你可能感兴趣的:(python爬取静态页面)