pythonは古詩文のウェブサイトの詩文の欄のすべての詩詞を登ります

4457 ワード

前に書く
かつて、私达はすべて梦があって、文学について、爱情について、1度の世界を通り抜ける旅行について
巻一
先日、古詩文サイトを発見し、至宝を手に入れたかのように、一時の私念に駆られて、その中の詩文の欄の文章を全部登ってきた.この一文を記す.
巻2
全体の過程を盗んだように、筋道がはっきりしていて、速戦速決です.詳しく聞いてください.
  • まず詩文欄のすべてのラベルのURLを取得し、その後ラベルに入り、すべての詩文詳細ページのURL
  • を取得する.
  • 各詳細ページの詳細、好きな情報を参照してください.例えば、テーマ、著者、内容
  • 取得した情報をデータベースに保存する
  • .
    巻三
    有用なパッケージのインポート
    #   
    import requests
    #      
    from lxml import etree
    #       ,           ,    
    from write_database import Write_databases
    

    クラスのコンストラクタ
    class GuShiWen():
        def __init__(self):
            self.main_url = 'https://www.gushiwen.org/'
            self.headers = {
                'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
            }
            self.bash_url = 'https://so.gushiwen.org/'
            #      
            self.database = Write_databases()
    

    まず、詩文欄のすべてのラベルのURLを取得します.
        def get_all_shiw_urls(self):
            res = requests.get(self.main_url,headers=self.headers)
            html = etree.HTML(res.text)
            sons_div_lists = html.xpath(".//div[@class='sons'][1]/div[@class='cont']/a")[:-2]
            for a_info in sons_div_lists:
                a_href = a_info.xpath('./@href')[0]
                a_text = a_info.xpath('./text()')
                self.get_all_content_urls(a_href)
    

    ラベル内のすべての詩文のurlを取得し、使用可能なURLとして構築します.
        def get_all_content_urls(self,urls):
            text_html = requests.get(urls,headers=self.headers)
            html = etree.HTML(text_html.text)
            text_title = html.xpath('.//div[@class="title"][1]/h1/text()')
            text_dev = html.xpath('.//div[@class="sons"][1]/div')
            for item in text_dev:
                text_span = item.xpath('./span')
                for span_item in text_span:
                    try:
                        text_a_href = span_item.xpath('./a/@href')[0]
                        text_a_text = span_item.xpath('.//text()')
                    except:
                        continue
                    self.get_poetry(self.bash_url + text_a_href)
    

    詩文の詳細を抽出し、データベースに書き込む
        def get_poetry(self,url):
            poetry_html = requests.get(url,headers=self.headers)
            html = etree.HTML(poetry_html.text)
            poetry_div = html.xpath('.//div[@class="sons"]/div')[0]
            poetry_title = poetry_div.xpath('./h1/text()')[0]
            poetry_author = poetry_div.xpath('./p//text()')
            poetry_author = " ".join(poetry_author)
            poetry_cont = poetry_div.xpath('./div[2]//text()')
            poetry_cont = " ".join(poetry_cont)
            print("====="*10+'===='+'===')
            print(poetry_title)
            print(poetry_author)
            print(poetry_cont)
            self.write_database(poetry_title,poetry_author,poetry_cont)
    
        def write_database(self,title,author,cont):
            self.database.insert_data(title,author,cont)
    

    最後にmain関数
    def main():
        gusw = GuShiWen()
        gusw.get_all_shiw_urls()
    

    巻四
    データベースクラスを実現して、主に含む機能は、データベースを接続して、登った情報をデータベースに書き込んで、ランダムにデータベースの中のある詩詞の情報を読み出して、データベースを閉じます
    import pymysql
    import random
    
    class Write_databases():
        def __init__(self):
            self.db = pymysql.connect(
                host = '127.0.0.1',
                user = 'root',
                password = 'root',
                database = 'gushiw',
                port = 3306
            )
            self.cursor = self.db.cursor()
    
        def insert_data(self,title,author,cont):
            sql = '''
                insert into gushiw_table(id,poetry_title,poetry_author,poetry_cont)
                values(null,%s,%s,%s)
            '''
            self.cursor.execute(sql,(title,author,cont))
            self.db.commit()
        def read_data(self):
            id = random.randint(127,4017)
            print(id)
            sql = 'select * from gushiw_table where id = %s'
    
            value = self.cursor.execute(sql,(id,))
    
            value = self.cursor.fetchall()
            print(value)
            title = value[0][1]
            author = value[0][2]
            cont = value[0][3]
            print(title,author,cont)
        def close_databases(self):
            self.db.close()
    

    続きを待たずにソフトウェアを書き、データベースの詩文をランダムに読み出し、表示します.