-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.py
More file actions
131 lines (115 loc) · 4.96 KB
/
test.py
File metadata and controls
131 lines (115 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import json
import time
# jetbrains://pycharm/navigate/reference?project=BQ_Spider&path=spider.html
# file:///H:/project/cursor/BQ_Spider/spider.html
class FenbiSpider:
def __init__(self):
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
self.driver = webdriver.Chrome(options=options)
self.wait = WebDriverWait(self.driver, 15)
def get_question_data(self, url):
self.driver.get(url)
time.sleep(3) # 等待页面基础加载
# 定位题目容器
question_container = self.wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div.ti-container.showBg")))
return {
"category": self._get_category(question_container),
"title": self._get_title(question_container),
"options": self._get_options(question_container),
"answer": self._get_correct_answer(question_container),
"right_rate": self._get_correct_rate(question_container),
"analysis": self._get_analysis(question_container),
"knowledge_point": self._get_knowledge_points(question_container),
"origin": self._get_origin(question_container)
}
def _get_category(self, container):
try:
return container.find_element(
By.CSS_SELECTOR, "div.title-type-name.ng-star-inserted").text.strip()
except NoSuchElementException:
return "未知题型"
def _get_title(self, container):
try:
content = container.find_element(
By.CSS_SELECTOR, "app-question-choice article.content")
return content.get_attribute("innerHTML").replace("\n", "</br>")
except NoSuchElementException:
return ""
def _get_options(self, container):
options = []
try:
options_container = container.find_element(
By.CSS_SELECTOR, "app-choice-radio ul.choice-radios")
options_elements = options_container.find_elements(
By.CSS_SELECTOR, "li.choice-radio")
for opt in options_elements:
# 处理包含图片的选项
img_tags = opt.find_elements(By.TAG_NAME, "img")
if img_tags:
options.append(
f'<img src="{img_tags[0].get_attribute("src")+"https:"}">')
else:
options.append(
opt.find_element(By.CSS_SELECTOR, "p.input-text").text)
except NoSuchElementException:
pass
return options
def _get_correct_answer(self, container):
try:
return container.find_element(
By.CSS_SELECTOR, "span.correct-answer.ng-star-inserted").text
except NoSuchElementException:
return ""
def _get_correct_rate(self, container):
try:
return container.find_element(
By.CSS_SELECTOR, "span.correct-rate").text.replace(" ", "") + "%"
except NoSuchElementException:
return ""
def _get_analysis(self, container):
try:
# 定位包含解析的section
section = container.find_element(
By.XPATH, ".//section[.//div[@class='solution-title' and text()='解析']]")
return section.find_element(
By.CSS_SELECTOR, "div.content").get_attribute("innerHTML")
except NoSuchElementException:
return ""
def _get_knowledge_points(self, container):
points = []
try:
keypoint_section = container.find_element(
By.CSS_SELECTOR, "app-solution-keypoint")
elements = keypoint_section.find_elements(
By.CSS_SELECTOR, "span.solution-keypoint-item-name")
points = [e.text for e in elements]
except NoSuchElementException:
pass
return points
def _get_origin(self, container):
try:
# 定位来源section
section = container.find_element(
By.XPATH, ".//section[.//div[@class='solution-title' and text()='来源']]")
return section.find_element(
By.CSS_SELECTOR, "div.content").text.strip()
except NoSuchElementException:
return ""
def run(self, url):
try:
data = self.get_question_data(url)
print(json.dumps(data, ensure_ascii=False, indent=2))
finally:
self.driver.quit()
if __name__ == "__main__":
spider = FenbiSpider()
target_url = "file:///H:/project/cursor/BQ_Spider/t.html"
# target_url = "https://spa.fenbi.com/ti/exam/solution/1_1_31k49p8?routecs=xingce"
spider.run(target_url)