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
| from Bio import Entrez from http.client import IncompleteRead import time
Entrez.email = "your_email@example.com"
step = 10 term = "((root) AND ((develop*) OR (growth))) AND (soil comp*)"
max_results = 17783 handle = Entrez.esearch(db="pubmed", term=term, retmax=max_results, usehistory="y") record = Entrez.read(handle) total = int(record['Count'])
webenv = record['WebEnv'] query_key = record['QueryKey']
with open('pubmed.txt', 'w', encoding="utf-8") as fw: for start in range(0, total, step): attempt = 0 success = False print("Download record %i to %i" % (start + 1, int(start+step))) while attempt < 5 and not success: try: handle = Entrez.efetch(db="pubmed", retstart=start, retmax=step, webenv=webenv, query_key=query_key, rettype="docsum", retmode="text") record = handle.read() fw.write(record) success = True except IncompleteRead as e: attempt += 1 print(f'IncompleteRead error:{e},Retrying ({attempt}/5)...') time.sleep(5) except Exception as e: print(f'Unexpected error!{e}') break
|