有什么办法可以加速我的python程序吗?

戈帕尔·辛格

我正在处理一个 pubmed 项目,我需要提取免费全文和免费 pmc 文章的 ID。这就是我的代码。

import requests
from bs4 import BeautifulSoup
from Bio import Entrez

Entrez.email = "[email protected]"     # Always tell NCBI who you are
handle = Entrez.esearch(db="pubmed", term="cough")
record = Entrez.read(handle)
count = record['Count']
handle = Entrez.esearch(db="pubmed", term="cough", retmax=count)
record = Entrez.read(handle)


free_article_ids = []
for id_ in record['IdList']:
    req = requests.get(f"https://www.ncbi.nlm.nih.gov/pubmed/{id_}")
    soup = BeautifulSoup(req.text, 'lxml')

    status = soup.find('span', {'class':'status_icon'})


    if status is None:
        continue
    elif status.text in ["Free full text", "Free PMC Article"]:
        free_article_ids.append(id_)
print(free_article_ids)

我的代码的问题是给出结果花费了太多时间,我想加快这个过程。我该怎么做?

大兵搜

使用多线程并发下载。推荐一个简单的框架。

from Bio import Entrez
from simplified_scrapy import Spider, SimplifiedDoc, SimplifiedMain
class MySpider(Spider):
  name = 'ncbi.nlm.nih.gov'
  start_urls = []

  def __init__(self):
    Entrez.email = "[email protected]"     # Always tell NCBI who you are
    handle = Entrez.esearch(db="pubmed", term="cough")
    record = Entrez.read(handle)
    count = record['Count']
    handle = Entrez.esearch(db="pubmed", term="cough", retmax=count)
    record = Entrez.read(handle)
    for id_ in record['IdList']:
      self.start_urls.append(f"https://www.ncbi.nlm.nih.gov/pubmed/{id_}")
    Spider.__init__(self,self.name) #necessary

  free_article_ids = []
  def extract(self,url,html,models,modelNames):
    doc = SimplifiedDoc(html)
    status = doc.select('span.status_icon')
    if status and status.text in ["Free full text", "Free PMC Article"]:
      id = url.split('/')[-1]
      self.free_article_ids.append(id)
      return {"Urls": [], "Data": {"id":id}}

    return True
SimplifiedMain.startThread(MySpider())

这里有更多的例子。https://github.com/yiyedata/simplified-scrapy-demo

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有什么办法可以“打破”程序吗?

有什么办法可以让我的 python 数字猜测器更有效率吗?

有什么办法可以让我的 python 数字猜测器更有效率吗?

有什么办法可以在特定的tty上打开程序吗?

有什么办法可以查看我的连接状况吗?

有什么办法可以使我的JFrame的中心居中吗?

有什么办法可以保存我的硬盘吗?

我想消除我在程序中绘制的桌子的重复外观。有什么办法可以将其放在单独的函数中吗?

有什么办法可以指向引用吗?

有什么办法可以杀死线程吗?

有什么办法可以延迟PaintComponent吗?

有什么办法可以翻译这个吗?

我应该期望该代码被阻止吗?有什么办法可以阻止它吗?

有什么办法可以查看C#应用程序的内存泄漏吗?

有什么办法可以在Android的Flutter应用程序中拦截“返回”键按下吗?

Ubuntu 19.10 升级毁了我的笔记本电脑。有什么办法可以恢复吗?

有什么办法可以隐藏我的真实版本的谷歌浏览器吗?

有什么办法可以解决我的服务器问候吗?

有什么办法可以让它在我的Linux mint destkop上下雪吗?

在使用 parseInt 时,我有什么办法可以**不**四舍五入吗?

有什么办法可以给我的应用程序一个图标?

我可以在还使用相机的Hololens中录制应用程序的视频吗?有办法做到这两者吗?

有什么办法可以改变python中ttk按钮的背景颜色吗?我尝试使用样式方法,但它只是更改了边框颜色

有什么办法可以使用python查找相对于矢量的标量值的导数吗?

有什么办法可以在Python多行字符串中使用变量吗?

有什么办法可以为整个软件包导入python模块吗?

有什么办法可以用python中的列表理解来编写这段代码吗?

有什么办法可以从网页上运行内部python脚本吗?

有什么办法可以在python中预处理这种数据吗?