栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用Python+xpath爬取Latex常用特殊符号小图标

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Python+xpath爬取Latex常用特殊符号小图标

import os
import requests
import random
from bs4 import BeautifulSoup
from lxml import etree
from cn_journal_user_agent import ua_list

# dir_list = os.listdir('latex_symbol')
dir_list = ['accents-in-latex', 'arrow-symbols-in-latex', 'binary-operation-symbols-in-latex', 'brackets-in-latex', 'constructions-in-latex', 'delimiters-in-latex', 'directional-symbols-in-latex', 'equality-and-inference-symbols-in-latex', 'geometry-notations-in-latex', 'greek-letters-in-latex', 'inequalities-in-latex', 'logic-notations-in-latex', 'mathematical-operator-symbols-in-latex', 'miscellaneous-symbols-in-latex', 'relational-operators-in-latex', 'set-notations-in-latex', 'trigonometric-functions-in-latex']

# 请求头
headers = {
    'User-Agent': random.choice(ua_list)
}

for di in dir_list:
    path = './symbols/' + di + '/'
    base_url = 'https://www.geeksforgeeks.org/' + di + '/'
    symbols_html = requests.post(base_url, headers=headers)
    symbols_html.encoding = 'utf-8'
    # symbols_DOM = etree.HTML(symbols_html.text)
    # symbols_name = symbols_DOM.xpath('*//div[@]/center/table/tbody/tr[2]/td[3]/text()')
    # print(symbols_html.text)
    soup = BeautifulSoup(symbols_html.text, 'html.parser')
    html_list = soup.find_all('tr')
    # print(html_list)
    tr_list = []
    for html in html_list:
        html_str = str(html).replace('n', '').replace('  ', '').replace('t', '')
        tr_list.append(html_str)

    code_dict = {}
    for tr in tr_list:
        DOM = etree.HTML(tr)
        symbols_name = DOM.xpath('//tr/td[1]/text()')
        if len(symbols_name) is not 0:
            # print(symbols_name[0])
            symbols_svg_url = DOM.xpath('//tr/td[2]/img/@src')[0]
            # print(symbols_svg_url)
            symbols_code = DOM.xpath('//tr/td[3]/text()')[0]
            # print(symbols_code)
            img = requests.get(symbols_svg_url)
            f = open(path + symbols_name[0] + '.jpg', 'ab')
            f.write(img.content)  # 多媒体存储content
            f.close()
            code_dict[symbols_name[0]] = symbols_code
            path = 'images/' + di + '/' + symbols_name[0] + '.jpg'
            print(symbols_code, path)
        else:
            pass
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/588069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号