HDUOJ题目HTML的爬取

HDUOJ题目HTML的爬取

封装好的exe/app的GitHub地址:https://github.com/Rhythmicc/HDUHTML 按照系统选择即可。

其实没什么难度,先爬下来一个题目的html,然后正则匹配一波塞个标签上去就好了。

下图运行效果:

HDUOJ题目HTML的爬取_第1张图片

下面是爬取下的HTML运行效果:

HDUOJ题目HTML的爬取_第2张图片

源码:

import re
import requests
from requests.exceptions import RequestException

url = "http://acm.hdu.edu.cn/showproblem.php?pid=" + input("HDU题号:")
headers = {
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15"}


def get_one_page(url, headers):
    try:
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            response.encoding = 'utf-8'
            return response.text
        return None
    except RequestException:
        return None


html = get_one_page(url, headers=headers)
tmp=re.findall('(.*?)',html,re.S)[0]
ans=re.findall('

',tmp,re.S)[0] print('

'+ans[0]+'


'+ans[1]) ask=input('按任意键退出')

View Code

求求你们放过我的博客吧,转载要注明出处呀。。

你可能感兴趣的:(HDUOJ题目HTML的爬取)