-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestapi.py
More file actions
723 lines (493 loc) · 20.2 KB
/
testapi.py
File metadata and controls
723 lines (493 loc) · 20.2 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
import os
import sys
import time
import json
import schedule
import requests
from bs4 import BeautifulSoup
from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel, QVBoxLayout, QPushButton, QWidget, QScrollArea)
from pystray import Icon, MenuItem, Menu
from PIL import Image
import win32com.client
import os
import json
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import re
response = requests.get("https://www.cell.com/cell/issue?pii=S0092-8674(24)X0002-1")
print(response.text)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless') # 无头模式
driver = webdriver.Chrome(options=options)
url = 'https://www.cell.com/cell/issue?pii=S0092-8674(24)X0002-1'
driver.get(url)
print(driver.page_source) # 获取页面内容
driver.quit()
assert(0)
# === CONFIGURATION ===
CONFIG_FILE = 'config.json'
CACHE_FILE = 'cache.json'
# === CACHE HANDLING ===
def save_cache(source, data):
cache = load_all_cache()
cache[source] = {"data": data, "timestamp": time.strftime('%Y-%m-%d')}
with open(CACHE_FILE, 'w') as f:
json.dump(cache, f)
def load_cache(source):
cache = load_all_cache()
return cache.get(source, {}).get("data", [])
def load_all_cache():
if os.path.exists(CACHE_FILE):
with open(CACHE_FILE, 'r') as f:
return json.load(f)
return {}
def is_cached(source):
cache = load_all_cache()
cached_date = cache.get(source, {}).get("timestamp", "")
return cached_date == time.strftime('%Y-%m-%d')
import math
from wordcloud import WordCloud
import matplotlib.pyplot as plt
def generate_wordclouds(set_column=4):
cache = load_all_cache()
if not cache:
print("No data in cache.")
return
row = math.ceil(len(cache) / set_column) # 向上取整,防止遗漏
figures, ax = plt.subplots(row, set_column, figsize=(set_column * 5, row * 3))
# 标准化 ax 为二维数组,防止出现一维或标量情况
if row == 1:
ax = [ax]
if set_column == 1:
ax = [[a] for a in ax]
row_number = 0
column_number = 0
for item in cache.keys():
data = load_cache(item)
if not data:
continue
text, *_ = zip(*data) # 只取第一列
titles = list(text)
text = " ".join(titles)
# 生成词云
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
# 绘制词云
ax[row_number][column_number].imshow(wordcloud, interpolation='bilinear')
ax[row_number][column_number].axis('off') # 关闭坐标轴
ax[row_number][column_number].set_title(item) # 添加标题
column_number += 1
if column_number == set_column: # 列满时换行
column_number = 0
row_number += 1
# 删除多余的空白子图
for i in range(row_number * set_column + column_number, row * set_column):
figures.delaxes(ax[i // set_column][i % set_column])
plt.tight_layout()
plt.show()
# 加载缓存
# 调用函数
generate_wordclouds()
assert(0)
import requests
import requests
from datetime import datetime, timedelta
import requests
from datetime import datetime, timedelta
import concurrent.futures
import requests
from datetime import datetime, timedelta
import concurrent.futures
import requests
from datetime import datetime, timedelta
import requests
from datetime import datetime, timedelta
def fetch_biorxiv_medrxiv():
try:
if is_cached('biorxiv_medrxiv'):
return load_cache('biorxiv_medrxiv')
# Get today's date
today = datetime.today()
# Calculate the date for one week ago from today
one_week_ago = today - timedelta(weeks=1)
# Format the dates to the required string format for the API (YYYY-MM-DD)
start_date = one_week_ago.strftime('%Y-%m-%d')
end_date = today.strftime('%Y-%m-%d')
papers = []
offset = 0 # Start from the first batch
while True:
# Create the URL with the dynamic date range and the offset
url = f"https://api.biorxiv.org/details/biorxiv/{start_date}/{end_date}/{offset}"
response = requests.get(url)
data = response.json()
# Check if there are any papers in the response
collection = data.get('collection', [])
if not collection:
break # Exit the loop if no more papers are found
# Process the papers in the current batch
for item in collection:
# Extract the DOI
doi = item.get('doi', 'N/A')
link = f"https://www.biorxiv.org/content/{doi}"
# Extract the title
title = item.get('title', 'N/A')
# Extract the publication date
date = item.get('date', 'N/A')
# Create the paper tuple and add it to the list
papers.append((title, link, date, []))
# Increment the offset for the next batch
offset += 100
print(offset)
# Save the result to cache
save_cache('biorxiv_medrxiv', papers)
return papers
except Exception as e:
return [(f"Error fetching bioRxiv/medRxiv: {str(e)}", "")]
import requests
from datetime import datetime, timedelta
from concurrent.futures import ThreadPoolExecutor, as_completed
def fetch_biorxiv_medrxiv():
try:
if is_cached('biorxiv_medrxiv'):
return load_cache('biorxiv_medrxiv')
# Get today's date
today = datetime.today()
# Calculate the date for one week ago from today
one_week_ago = today - timedelta(weeks=1)
# Format the dates to the required string format for the API (YYYY-MM-DD)
start_date = one_week_ago.strftime('%Y-%m-%d')
end_date = today.strftime('%Y-%m-%d')
papers = []
max_threads = 3 # Number of threads to use
offset = 0 # Start from the first batch
def fetch_batch(offset):
# Create the URL with the dynamic date range and the offset
url = f"https://api.biorxiv.org/details/biorxiv/{start_date}/{end_date}/{offset}"
response = requests.get(url)
data = response.json()
# Process the papers in the current batch
batch_papers = []
collection = data.get('collection', [])
for item in collection:
# Extract the DOI
doi = item.get('doi', 'N/A')
link = f"https://www.biorxiv.org/content/{doi}"
# Extract the title
title = item.get('title', 'N/A')
# Extract the publication date
date = item.get('date', 'N/A')
# Create the paper tuple and add it to the list
batch_papers.append((title, link, date, []))
return batch_papers
with ThreadPoolExecutor(max_workers=max_threads) as executor:
futures = []
while True:
# Dispatch multiple requests
for i in range(max_threads):
futures.append(executor.submit(fetch_batch, offset))
offset += 100 # Increment offset for the next batch
print(offset)
# Wait for all threads to finish
for future in as_completed(futures):
papers.extend(future.result())
# If no more papers are fetched, break the loop
if len(futures) < max_threads or len(futures[-1].result()) == 0:
break
# Save the result to cache
save_cache('biorxiv_medrxiv', papers)
return papers
except Exception as e:
return [(f"Error fetching bioRxiv/medRxiv: {str(e)}", "")]
print(fetch_biorxiv_medrxiv())
assert(0)
from paperscraper.get_dumps import biorxiv, medrxiv, chemrxiv
biorxiv(start_date="2025-03-01", end_date="2025-03-02")
assert(0)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
# 设置 WebDriver 路径
driver = webdriver.Chrome()
# 打开目标网页
driver.get("https://www.biorxiv.org/content/early/recent?page=3")
# 等待页面加载
driver.implicitly_wait(10)
# 找到并模拟点击
button = driver.find_element(By.ID, "button-id") # 根据页面元素定位
action = ActionChains(driver)
action.click(button).perform()
# 等待并抓取网页内容
print(driver.page_source)
# 关闭浏览器
driver.quit()
assert(0)
from playwright.sync_api import sync_playwright
def scrape():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context(
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
page = context.new_page()
page.goto("https://www.biorxiv.org/content/early/recent?page=3", timeout=60000)
# 等待页面加载并点击按钮
try:
page.wait_for_selector("text='Verify'", timeout=20000)
page.click("text='Verify'")
print("点击按钮成功")
except Exception as e:
print(f"未找到按钮或点击失败: {e}")
# 获取HTML内容
html = page.content()
print(html)
browser.close()
from playwright.sync_api import sync_playwright
from playwright_stealth import stealth
import time
from playwright.sync_api import sync_playwright
from playwright_stealth import stealth_sync # 使用正确的导入
import time
def scrape():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context(
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
page = context.new_page()
# 使用 stealth_sync 来应用反检测功能
stealth_sync(page)
# 访问目标页面
page.goto("https://www.biorxiv.org/content/early/recent?page=3", timeout=60000)
try:
# 等待并点击标签
label = page.locator('label.cb-lb')
label.wait_for(state="visible", timeout=10000)
label.click()
print("点击标签成功")
# 等待页面加载完成
page.wait_for_load_state('networkidle')
except Exception as e:
print(f"未找到标签或点击失败: {e}")
# 获取HTML内容
html = page.content()
print(html)
browser.close()
scrape()
assert(0)
import requests
from bs4 import BeautifulSoup
import concurrent.futures
import time
# 每批抓取 10 页,每次启动 10 个线程
BATCH_SIZE = 10
MAX_THREADS = 10
MAX_PAGES = 100 # 最多抓取 100 页
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
# 配置浏览器
options = webdriver.ChromeOptions()
options.add_argument("--headless") # 不显示窗口
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
# 启动浏览器
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
# 打开目标网页
url = "https://www.biorxiv.org/content/early/recent?page=1"
driver.get(url)
# 获取加载后的网页内容
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
# 打印内容
print(soup.prettify())
# 关闭浏览器
driver.quit()
with open("example.html",'w',encoding='utf-8') as f:
f.write(str(soup))
def fetch_page_biorxiv(page_number):
try:
url = f"https://www.biorxiv.org/content/early/recent?page={page_number}"
response = requests.get(url, timeout=10)
if response.status_code != 200:
print(f"⚠️ 第 {page_number} 页获取失败,状态码: {response.status_code}")
return []
soup = BeautifulSoup(response.text, 'html.parser')
with open("example.html",'w',encoding='utf-8') as f:
f.write(str(soup))
articles = soup.find_all('div', class_='highwire-cite highwire-cite-highwire-article highwire-citation-biorxiv-article-pap-list clearfix')
if not articles:
print(f"⚠️ 第 {page_number} 页没有找到文章,可能是末页。")
return []
papers = []
for article in articles:
# 标题和链接
link_tag = article.find('a', class_='highwire-cite-linked-title')
if link_tag and 'href' in link_tag.attrs:
link = f"https://www.biorxiv.org{link_tag['href']}"
title = link_tag.find('span', class_='highwire-cite-title').get_text(strip=True)
else:
continue
# 摘要
summary_tag = article.find('div', class_='highwire-cite-snippet')
summary = summary_tag.get_text(strip=True) if summary_tag else "No summary available"
# 发布日期
date_tag = article.find('span', class_='highwire-cite-metadata-date')
date_published = date_tag.get_text(strip=True) if date_tag else "No date available"
papers.append((title, link, summary, date_published))
return papers
except Exception as e:
print(f"❌ 抓取第 {page_number} 页出错: {e}")
return []
def fetch_biorxiv():
try:
if is_cached('biorxiv'):
return load_cache('biorxiv')
papers = []
page_number = 1
while True:
print(f"🚀 开始抓取第 {page_number} 页到第 {page_number + BATCH_SIZE - 1} 页")
batch_results = []
stop_flag = False
# 使用 ThreadPoolExecutor 进行并行抓取
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
future_to_page = {
executor.submit(fetch_page_biorxiv, page): page
for page in range(page_number, page_number + BATCH_SIZE)
}
for future in concurrent.futures.as_completed(future_to_page):
page = future_to_page[future]
try:
result = future.result()
if not result:
print(f"⚠️ 第 {page} 页为空,可能是末页,准备停止抓取。")
stop_flag = True
else:
batch_results.extend(result)
except Exception as e:
print(f"❌ 抓取第 {page} 页失败: {e}")
# 合并当前批次结果
papers.extend(batch_results)
if stop_flag or page_number + BATCH_SIZE > MAX_PAGES:
print("✅ 抓取完成!")
break
# 更新下一批抓取的起点
page_number += BATCH_SIZE
# 保存结果(缓存)
save_cache('biorxiv', papers)
return papers
except Exception as e:
print(f"❌ Error fetching biorxiv: {e}")
return [(f"Error fetching biorxiv: {str(e)}", "", "", "")]
# 调用函数
if __name__ == "__main__":
start_time = time.time()
papers = fetch_biorxiv()
end_time = time.time()
print(f"✅ 抓取完成,共 {len(papers)} 篇文章,耗时 {end_time - start_time:.2f} 秒")
# 打印前 5 条记录
for paper in papers[:5]:
print(f"📌 标题: {paper[0]}\n🔗 链接: {paper[1]}\n📄 摘要: {paper[2]}\n📅 日期: {paper[3]}\n{'-'*50}")
assert(0)
import requests
from bs4 import BeautifulSoup
def fetch_nature_biotechnology():
try:
if is_cached('nature_biotechnology'):
return load_cache('nature_biotechnology')
url = "https://www.nature.com/nbt/articles?year=2025"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
papers = []
# 找到所有文章的 li 标签
articles = soup.find_all('li', class_='app-article-list-row__item')
for article in articles:
# print(article)
# 提取文章的链接
# link = f"https://www.nature.com{article.find('a', class_='c-card__link u-link-inherit')['href']}"
link_tag = article.find('a', class_='c-card__link u-link-inherit')
if link_tag and 'href' in link_tag.attrs:
link = f"https://www.nature.com{link_tag['href']}"
else:
# 如果链接格式不符合预期,则跳出循环
print("Link format not as expected, skipping article.")
break
# 提取标题
title = article.find('a', class_='c-card__link u-link-inherit').get_text(strip=True)
# 可选:提取摘要
summary = article.find('div', class_='c-card__summary') # 定位到包含摘要的 div 标签
if summary:
p_tag = summary.find('p') # 找到 div 标签中的 p 标签
summary_text = p_tag.get_text(strip=True) if p_tag else "No summary available"
else:
summary_text = "No summary available"
# 提取发布日期
date_published = article.find('time', class_="c-meta__item c-meta__item--block-at-lg" ).get_text(strip=True)
papers.append((title, link, summary_text, date_published))
# assert()
print(papers)
assert()
save_cache('nature_biotechnology', papers)
return papers
except Exception as e:
return [(f"Error fetching Nature Biotechnology: {str(e)}", "", "", "")]
# url = "https://www.nature.com/nbt/articles?year=2025"
# response = requests.get(url)
# soup = BeautifulSoup(response.text, 'html.parser')
# papers = []
# # 找到所有文章的 li 标签
# articles = soup.find_all('li', class_='app-article-list-row__item')
# for article in articles:
# # print(article)
# # 提取文章的链接
# link = f"https://www.nature.com{article.find('a', class_='c-card__link u-link-inherit')['href']}"
# # 提取标题
# title = article.find('a', class_='c-card__link u-link-inherit').get_text(strip=True)
# # 可选:提取摘要
# summary = article.find('div', class_='c-card__summary') # 定位到包含摘要的 div 标签
# if summary:
# p_tag = summary.find('p') # 找到 div 标签中的 p 标签
# summary_text = p_tag.get_text(strip=True) if p_tag else "No summary available"
# else:
# summary_text = "No summary available"
# # 提取发布日期
# date_published = article.find('time', class_="c-meta__item c-meta__item--block-at-lg" ).get_text(strip=True)
# papers.append((title, link, summary_text, date_published))
# # assert()
# print(papers)
# assert()
print(fetch_nature_biotechnology())
assert()
# === test part===
import requests
from bs4 import BeautifulSoup
def fetch_nature_biotech():
try:
if is_cached('nature_biotech'):
return load_cache('nature_biotech')
url = "https://www.nature.com/nbt/articles?year=2025"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
print(soup)
papers = []
articles = soup.find_all('article', class_='card')
for article in articles:
link = article.find('a', class_='card-title')['href']
title = article.find('a', class_='card-title').get_text(strip=True)
papers.append((title, f"https://www.nature.com{link}"))
save_cache('nature_biotech', papers)
return papers
except Exception as e:
return [(f"Error fetching Nature Biotechnology: {str(e)}", "")]
url = "https://www.nature.com/nbt/articles?year=2025"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
with open("output.html", "w", encoding="utf-8") as f:
f.write(soup.prettify())
print(soup)
# articles = soup.find_all('article', class_='card')
# print(articles )
# print(fetch_nature_biotech())