目录
项目团队
DrugLLM开发团队
日期
2024/5/26
本周进度
编写一个爬虫代码,用于爬取论文数据
import requests
from bs4 import BeautifulSoup
def fetch_paper_titles(url):
try:
# 发送HTTP请求
response = requests.get(url)
# 确保请求成功
if response.status_code == 200:
# 使用BeautifulSoup解析HTML响应
soup = BeautifulSoup(response.text, 'html.parser')
# 假设论文标题包含在class为'paper-title'的HTML元素中
title_elements = soup.find_all('div', class_='paper-title')
# 提取并打印所有论文标题
titles = [title.get_text(strip=True) for title in title_elements]
return titles
else:
print("请求失败,状态码:", response.status_code)
except Exception as e:
print("发生错误:", e)
url = "https://paperswithcode.com/"
# 调用函数并打印结果
titles = fetch_paper_titles(url)
for title in titles:
print(title)