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[@class="text"]/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
使用Python+xpath爬取Latex常用特殊符号小图标
最新推荐文章于 2023-05-18 15:05:52 发布