杭电网安网络编程理论实验


import requests
from bs4 import BeautifulSoup


class Crawler:
    result = ''
    result1 = []
    paraphrase0 = []
    paraphrase1 = []
    doubleparaphrase = []
    EEparaphrase0 = []
    y0 = []
    y1 = []
    flag=0

    def __init__(self, param):
        try:
            url = 'http://dict.cn/{}'.format(param)
            print(url)
            r = requests.get(url)
            html = BeautifulSoup(r.text, "lxml")
            self.result1=[]
            self.result = html.select('body > div#content > div.main > div.word > div.word-cont > h1.keyword')[
                0].text  ##单词本身
            print(self.result)
            for bdo in html.select('body > div#content > div.main >   div.word > div.phonetic > span > bdo'):
                self.result1.append(bdo.get_text())##单词音标

            ##paraphrase0保存解释的词性,paraphrase1以列表方式保存每个词性对应的解释
            self.paraphrase0 = []
            for span in html.select('body > div#content > div.main >   div.section.def > div.layout.detail > span '):
                self.paraphrase0.append(span.get_text())
            print(self.paraphrase0)
            self.paraphrase1 = []
            for li in html.select('body > div#content > div.main >   div.section.def > div.layout.detail > ol '):
                self.paraphrase1.append(li.get_text())
            print(self.paraphrase1)
            self.doubleparaphrase = []
            # example1=[]
            # for li in html.select('body > div#content > div.main >   div.section.def > div.layout.dual > ol >li'):
            #     example1.append((li.select('strong')[0].get_text()))
            # print(example1)
            for li in html.select(
                    'body > div#content > div.main >   div.section.def > div.layout.dual > ol >li'):  ###双语释义
                self.doubleparaphrase.append((li.get_text()))
            print(self.doubleparaphrase)
            self.EEparaphrase0 = []
            for li in html.select('body > div#content > div.main >   div.section.def > div.layout.en > ol >li'):  ##英英释义
                self.EEparaphrase0.append(li.get_text())
            print(self.EEparaphrase0)
            self.y0 = []  ##例句中该单词词性
            for b in html.select('body > div#content > div.main >   div.section.sent > div.layout.sort > div >b'):
                self.y0.append(b.get_text())
            print(self.y0)
            self.y1 = []  ##例句本身
            for ol in html.select('body > div#content > div.main >   div.section.sent > div.layout.sort > ol'):
                self.y1.append(ol.get_text().split("\n"))
            print(self.y1)
            self.flag=1
        except:
            self.flag=0

webcrawler.py

from flask import Flask, render_template, request
import webcrawler
app = Flask(__name__)


@app.route('/',methods=['POST','GET'])
def hello_world():
    return render_template('index0.html')


@app.route('/select', methods=['POST'])
def select():
    x=webcrawler.Crawler(request.form["words"])
    if x.flag==1:
        return render_template('index1.html',result=x.result,paraphrase0=x.paraphrase0,paraphrase1=x.paraphrase1,CE=x.doubleparaphrase,EE=x.EEparaphrase0,y0=x.y0,y1=x.y1,result1=x.result1)
    else:
        return "查询失败"

if __name__ == '__main__':
    app.run(debug=True)

webpage.py

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>英语字典</title>
</head>
<body id="body">
<style>

.div1 {
    height: 180px;
    width: 380px;
    border: 1px solid #8A8989;
    margin: 0 auto;
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
}
.input {
    display: block;
    width: 350px;
    height: 40px;
    margin: 10px auto;
}
.button {
    background: #2066C5;
    color: white;
    font-size: 18px;
    font-weight: bold;
    height: 50px;
    border-radius: 4px;
}
</style>
<div class="div1">
{#  action:接受的路由  #}
    <form action="/select" method="post">
        <input type="text" class="input" name="words" placeholder="请输入单词">
        <input type="submit" value="查询" class="input button">
    </form>
</div>
</body>
</html>

index0.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{{result}}</title>
    </head>
    <style>
        html,body{
                height:100%;
            }
        body{

	}
        p    {color: red;}
        .button {
    background: #2066C5;
    color: white;

}
</style>
<body id="body">
    <p style="font-size: 30px"><strong>音标</strong></p>
        <table id="cparaphrase">
            <tr>
                <td style="font-size: 20px">英式发音</td ><td style="font-size: 20px">美式发音</td>
            </tr>
            <tr>
                {% for i in result1 %}
            <td>{{i}}</td>
                {% endfor %}
            </tr>
        </table>
    <p style="font-size: 30px"><strong>中文释义</strong></p>
    <table id="cparaphrase0">
        <tr>
            <td>
                <table id="cparaphrase1">
                    {% for i in paraphrase0 %}
                    <tr>

                    <th>{{i}}</th>

                    </tr>

                {% endfor %}
                </table>
            </td>
            <td>
        <table id="cparaphrase2">
            {% for i in paraphrase1 %}
            <tr>

            <td>{{i}}</td>

            </tr>
            {% endfor %}
        </table>
            </td>
        </tr>
    </table>
    <p style="font-size: 30px"><strong>中英释义</strong></p>
    <table id="ceparaphrase">
        {% for i in CE %}
        <tr>

        <th>{{i}}</th>

        </tr>
        {% endfor %}
    </table>
    </table>
    <p style="font-size: 30px"><strong>英英释义</strong></p>
    <table id="eeparaphrase">
        {% for i in EE %}
        <tr>

        <td>{{i}}</td>

        </tr>
        {% endfor %}
    </table>



    <p style="font-size: 30px">
        <strong>例句</strong>
    </p>
    <table id="exqmple0">
        <tr>
            <td>
                <table id="example1">
                    {% for i in y0 %}
                    <tr>

                    <th>{{i}}</th>

                    </tr>

                {% endfor %}
                </table>
            </td>
            <td>
        <table id="example2">
            {% for i in y1 %}
            {%for j in i%}
            <tr>
            <td>{{j}}</td>
            </tr>
            {% endfor %}
            {% endfor %}
        </table>
            </td>
        </tr>
    </table>

</body>
</html>

index1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值