-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathget_data.py
More file actions
131 lines (114 loc) · 5.19 KB
/
get_data.py
File metadata and controls
131 lines (114 loc) · 5.19 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 urllib.parse import urlencode, quote
import requests
from bs4 import BeautifulSoup
import pandas as pd
from messages import summary_paper
def translate(text):
# 查询参数字典
params = {'wd': f'{text}'}
# 对查询参数进行百分号编码
encoded_params = urlencode(params, quote_via=quote)
url_relevance = (f'https://xueshu.baidu.com/s?{encoded_params}&pn={0}&tn=SE_baiduxueshu_c1gjeupa&'
f'ie=utf-8&sc_f_para=sc_tasktype%3D%7BfirstSimpleSearch%7D&sc_hit=1')
url_reference = (f'https://xueshu.baidu.com/s?{encoded_params}&pn={0}&tn=SE_baiduxueshu_c1gjeupa&'
f'ie=utf-8&sort=sc_cited&sc_f_para=sc_tasktype%3D%7BfirstSimpleSearch%7D&sc_hit=1')
return url_relevance, url_reference
def scrape_data(url, n):
data_list = []
for page in range(n):
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
data = soup.find_all('h3', class_='t c_font')
href_list = []
for temp in data:
href_list.append(temp.a.get('href'))
for href in href_list:
req = requests.get(href)
# 检查响应状态码
if req.status_code == 404:
print(f"404 error: {url} not found, skipping.")
continue # 跳过本次循环
req_soup = BeautifulSoup(req.text, 'html.parser')
print('\ntittle 的 href:')
print(href)
print('\n')
tittle = req_soup.find('div', class_='main-info').find('a').text.strip()
key_word = req_soup.find('div', class_='kw_wr')
if key_word:
key_word = key_word.find('p', class_=lambda x: x in ['kw_main', 'kw_main_s'])
if key_word:
key_word = key_word.text.strip()
else:
key_word = '暂无'
else:
key_word = '暂无'
# key_word = req_soup.find('div', class_='kw_wr').find('p', class_=lambda x: x in ['kw_main', 'kw_main_s']).find('a').text.strip()
abstract = req_soup.find('p', class_='abstract')
if abstract:
abstract = abstract.text.strip()
else:
abstract = '暂无'
# abstract = req_soup.find('p', class_='abstract').text.strip()
# doi = req_soup.find('div', class_='doi_wr').find('p', class_='kw_main').text.strip()
doi = req_soup.find('div', class_='doi_wr')
if doi:
print('\n')
print(href)
print(doi)
doi = doi.find('p', class_=lambda x: x in ['kw_main', 'kw_main_s'])
if doi:
doi = doi.text.strip()
else:
doi = '暂无'
else:
doi = '暂无'
author = req_soup.find('div', class_='author_wr')
if author:
author = author.find('p', class_='author_text')
if author:
author = author.text.strip()
else:
author = '暂无'
else:
author = '暂无'
# author = req_soup.find('div', class_='author_wr').find('p', class_='author_text kw_main_s').text.strip()
paper_url = href
data_list.append([tittle, key_word, abstract, doi, author, paper_url])
print(url)
url = url.replace(f'pn={page}', f'pn={page + 10}')
print(url)
total_data = pd.DataFrame(data_list, columns=['Tittle', 'Keywords', 'Abstract', 'DOI/ISBN', 'Author', 'Paper_url'])
return total_data
# 定义一个函数来格式化输出
# 定义一个函数来格式化输出特定的列
def format_specific_columns(llm_key, row, columns):
formatted_string = ""
for col in columns:
formatted_string += f"{col}: {row[col]} "
# print("大模型输入的是:\n", formatted_string)
# print("大模型输出的是:\n", fold_paper(formatted_string.strip()))
return summary_paper(key=llm_key, message=formatted_string.strip())
def summarize_data(llm_key, data):
fold_data = data[['Tittle', 'Keywords', 'Abstract', 'DOI/ISBN', 'Author', 'Paper_url']]
abstract = []
for i in range(len(fold_data)):
abstract.append(format_specific_columns(llm_key, fold_data.iloc[i], ['Tittle', 'Abstract']))
# 应用格式化函数并输出特定的列
# print(f"第{i}次abstract输出:\n", abstract)
fold_data['Abstract'] = abstract
fold_data.rename(columns={'Abstract': 'LLM Summary'}, inplace=True)
return fold_data
def run(llm_key, text, n):
url1, url2 = translate(text)
relevance_data = scrape_data(url1, n)
reference_data = scrape_data(url2, n)
relevance_data.to_csv('relevance_data.csv', encoding='utf-8-sig')
reference_data.to_csv('reference_data.csv', encoding='utf-8-sig')
new_relevance_data = summarize_data(llm_key, relevance_data)
new_relevance_data.to_csv('new_relevance_data.csv', encoding='utf-8-sig')
new_reference_data = summarize_data(llm_key, reference_data)
new_reference_data.to_csv('new_reference_data.csv', encoding='utf-8-sig')
return new_relevance_data, new_reference_data
# text = 'CNN'
# n = 2
# run(text, n)