初学python爬虫,记录一下第一个程序

这个程序是实现对NITOJ题面信息的简单爬取

因为有些题目的题面,input,output的描述是用<p>标签,有些用<div>标签,有些不用标签,直接写在大的<div>下的,而这程序只识别<p>标签下的文本,所以就有了那个奇怪的话=。=(对不起,请换一个题)。

因为初学,所以程序比较残疾

因为是第一次实现,所以想记录一下

路过大佬请无视

""" 
    2019/8/9
    version: 1.0.0
    by Zeronera
    实现对NITOJ题面,输入输出的简单爬取
"""

import requests
from bs4 import BeautifulSoup


def get_problem_text(id):

    url = 'https://www.nitacm.com/problem_show.php?pid=' + id
    r = requests.get(url, timeout=30)
    soup = BeautifulSoup(r.text, 'lxml')
    div_list = soup.find_all('div', {'class': 'content-wrapper well'})
    problem_content = div_list[0].find_all('p')
    flag = 0
    print("题面描述:")
    for i in problem_content:
        print(i.text)
        flag = 1
    problem_input = div_list[1].find_all('p')
    print("Input:")
    for i in problem_input:
        print(i.text)
        flag = 1
    problem_output = div_list[2].find_all('p')
    print("Output:")
    for i in problem_output:
        print(i.text)
        flag = 1
    if flag == 0:
        print("对不起,请换一个题")


def main():

    while True:
        problem_id = input('题目编号[100-22815]:')
        get_problem_text(problem_id)
        print('\n')
        t = input("输入exit退出程序,输入其他字符继续运行程序:")
        if t == 'exit':
            break


if __name__ == '__main__':
    main()

 

转载于:https://www.cnblogs.com/Zeronera/p/11325995.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值