Skip to content

Commit 2867514

Browse files
committed
[leetcode] fix no find description
1 parent ee48120 commit 2867514

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

leetcode_problems.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#!/usr/bin/env python
2+
# -*- coding=utf-8 -*-
23

34
import sys
45
import re
56
import os
67
import argparse
78
import requests
8-
from lxml import html
9+
from lxml import html as lxml_html
10+
11+
try:
12+
import html
13+
except ImportError:
14+
import HTMLParser
15+
html = HTMLParser.HTMLParser()
916

1017
try:
1118
import cPickle as pk
@@ -23,7 +30,7 @@ def get_problems_info(self):
2330
indexs = re.findall(r'<td>(\d+)</td>', cm)
2431
problem_urls = ['https://leetcode.com' + url \
2532
for url in re.findall(
26-
r'href="(/problems/.+?)"', cm)]
33+
r'<a href="(/problems/.+?)"', cm)]
2734
levels = re.findall(r"<td value='\d*'>(.+?)</td>", cm)
2835
tinfos = zip(indexs, levels, problem_urls)
2936
infos = []
@@ -32,11 +39,14 @@ def get_problems_info(self):
3239
if not res.ok:
3340
print('request error')
3441
sys.exit()
35-
tree = html.fromstring(res.text)
42+
tree = lxml_html.fromstring(res.text)
3643
title = tree.xpath('//meta[@property="og:title"]/@content')[0]
37-
description = tree.xpath('//meta[@property="og:description"]/@content')[0]
38-
if self.args.rm_blank:
39-
description = re.sub(r'\n+', r'\n', description)
44+
description = tree.xpath('//meta[@property="description"]/@content')
45+
if not description:
46+
description = tree.xpath('//meta[@property="og:description"]/@content')[0]
47+
else:
48+
description = description[0]
49+
description = html.unescape(description.strip())
4050
tags = tree.xpath('//div[@id="tags"]/following::a[@class="btn btn-xs btn-primary"]/text()')
4151
infos.append(
4252
{
@@ -71,6 +81,8 @@ def to_text(self, pm_infos):
7181
'{description}\n' + '\n' * self.args.line
7282
text = ''
7383
for info in infos:
84+
if self.args.rm_blank:
85+
info['description'] = re.sub(r'[\n\r]+', r'\n', info['description'])
7486
text += text_template.format(**info)
7587

7688
with open('leecode problems.txt', 'w') as g:

0 commit comments

Comments
 (0)