用python爬取网页里的文献,通过关键字筛选,然后下载到指定文件夹里
运行后无反应,要怎么修改才行
import requests
from bs4 import BeautifulSoup
import os
# 定义要爬取的页面URL
url = 'https://sc.panda321.com/scholar?start=10&q=insulin+resistance&hl=zh-CN&as_sdt=0,5' # 替换为实际网页的URL
keyword = 'insulin' # 替换为实际的关键字
save_folder = 'E:\生物11' # 替换为实际的文件夹路径
# 创建保存文件夹(如果不存在)
if not os.path.exists(save_folder):
os.makedirs(save_folder)
# 发起GET请求获取页面内容
response = requests.get(url)
html = response.text
# 使用BeautifulSoup解析页面
soup = BeautifulSoup(html, 'html.parser')
# 定位到文献列表的HTML元素
doc_list = soup.find_all('a', class_='doc-link')
# 遍历文献列表
for doc in doc_list:
# 提取文献链接和标题
doc_url = doc['href']
doc_title = doc.text.strip()
# 判断关键字是否在文献标题中
if keyword in doc_title:
# 下载文献并保存到指定文件夹
print('正在下载文献:', doc_title)
try:
response = requests.get(doc_url)
with open(os.path.join(save_folder, doc_title + '.pdf'), 'wb') as file:
file.write(response.content)
print('下载完成')
except Exception as e:
print('下载失败:', str(e))
根据你描述的问题,主要的原因可能有以下几点:
没反应是啥意思。错误信息有吗
这是老师上课讲的图解,仅供参考,应该是看不懂的
具体代码实现
"""
@version:
@filename : 链表反转
@author : liuhongjie
@projectname: day02
@time: 2022/05/30
"""
#定义一个节点类,用来等一下实现链表的创建
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
#实现链表的反转
def reverseList(self, head):
pre, cur = None, head
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
#把反转以后的链表打印出来
def print_linked_list(head):
if not head or not head.next:
return []
result = []
while head:
result.append(head.val)
head = head.next
return result
# 调用节点类,创建一个链表: a->b->c,分别对应的就是:1,2,3
a = ListNode(1)
b = ListNode(2)
c = ListNode(3)
a.next = b
b.next = c
#创建一个实例化对象
demo = Solution()
print(print_linked_list(demo.reverseList(a)))