利用关键词批量下载CVPR论文
思路先筛选出带关键词的论文然后再批量下载筛选后的论文文件省得自己一个个点击PDF进行下载了这里的CVPR2025.html可以直接下载官网页面https://openaccess.thecvf.com/CVPR2025?dayallimport os import re import csv import requests from urllib.parse import urljoin from bs4 import BeautifulSoup # 需要你自行配置的参数 HTML_PATH ./CVPR2025.html KEYWORD Text-to-Video # 关键词不区分大小写 OUTPUT_CSV cvpr_filtered_papers.csv DOWNLOAD_DIR cvpr_pdfs # 如果页面里 PDF 链接是相对路径这里填页面的基地址如果全是 https:// 开头就可以留空 BASE_URL # 比如: https://openaccess.thecvf.com/ # def extract_papers_with_keyword(html_path, keyword, base_url): 从本地 HTML 中提取标题包含 keyword 的论文及其 PDF 链接。 返回列表: [{keyword:..., title:..., pdf_url:...}, ...] with open(html_path, r, encodingutf-8, errorsignore) as f: html f.read() soup BeautifulSoup(html, lxml) results [] keyword_lower keyword.lower() # CVPR OpenAccess 的结构是按 dt classptitle 标题后面若干 dd 里包含 pdf 链接 # 因此这里直接按每个 dt 去找其后面的 pdf for dt in soup.find_all(dt, class_ptitle): title_a dt.find(a) if not title_a: continue title_text title_a.get_text(stripTrue) if not title_text: continue # 先按标题过滤关键词再找 pdf效率更高 if keyword_lower not in title_text.lower(): continue pdf_url None # 在当前 dt 之后的兄弟节点通常是若干个 dd中查找第一个 .pdf 链接 # 一直找到下一个 dt 为止 sibling dt.next_sibling while sibling is not None and getattr(sibling, name, None) ! dt: if getattr(sibling, name, None) dd: pdf_a sibling.find(a, hrefre.compile(r\.pdf$, re.IGNORECASE)) if pdf_a: pdf_href pdf_a.get(href, ).strip() if pdf_href: pdf_url urljoin(base_url, pdf_href) if base_url else pdf_href break sibling sibling.next_sibling # 如果没找到 pdf 链接就跳过这篇 if not pdf_url: continue results.append({ keyword: keyword, title: title_text, pdf_url: pdf_url, }) return results def save_results_to_csv(records, csv_path): 把筛选出的论文信息保存到 CSV 文件。 if not records: print(没有找到匹配关键词的论文未生成 CSV。) return fieldnames [keyword, title, pdf_url] with open(csv_path, w, newline, encodingutf-8) as f: writer csv.DictWriter(f, fieldnamesfieldnames) writer.writeheader() for r in records: writer.writerow(r) print(f已将 {len(records)} 条记录写入: {csv_path}) def download_pdfs_from_csv(csv_path, download_dir): 从 CSV 中读取 pdf_url 字段逐个下载 PDF 到本地目录。 if not os.path.exists(csv_path): print(fCSV 文件不存在: {csv_path}) return os.makedirs(download_dir, exist_okTrue) with open(csv_path, r, encodingutf-8) as f: reader csv.DictReader(f) for i, row in enumerate(reader, start1): pdf_url row.get(pdf_url, ).strip() title row.get(title, ).strip() or fpaper_{i} if not pdf_url: print(f[跳过] 第 {i} 行没有 pdf_url) continue # 用标题生成文件名避免非法字符 safe_title re.sub(r[\\/*?:|], _, title)[:150] filename f{safe_title}.pdf filepath os.path.join(download_dir, filename) # 如果已经下载过跳过 if os.path.exists(filepath): print(f[已存在] {filepath}) continue print(f[下载中] {pdf_url} - {filepath}) try: resp requests.get(pdf_url, timeout60) resp.raise_for_status() with open(filepath, wb) as out: out.write(resp.content) except Exception as e: print(f[失败] 无法下载 {pdf_url}{e}) def main(): # 第一步从 HTML 提取符合关键词的论文信息 records extract_papers_with_keyword(HTML_PATH, KEYWORD, base_urlBASE_URL) print(f找到 {len(records)} 篇标题包含关键词 {KEYWORD} 的论文。) # 第二步保存到 CSV save_results_to_csv(records, OUTPUT_CSV) # 第三步根据 CSV 批量下载 PDF download_pdfs_from_csv(OUTPUT_CSV, DOWNLOAD_DIR) if __name__ __main__: main()