Bulk download papers from scihub for text mining

https://janikvonrotz.ch/2020/05/07/bulk-download-papers-from-scihub-for-text-mining/

Last evening I wanted to play some games with my brother. Instead we ended up writing a simple script to bulk download papers from scihub. He studies bioinformatics and is currently doing some meta research in his field. By crawling a publication database by specific keywords he got list of papers which need to be analyzed. However, most of the paper are hidden behind pay walls. Luckily there’s scihub. The most hated and beloved platform to get your hands on almost any scientific paper. He asked me wether I could help him building script that downloads papers from scihub based on a list of dois.

Sure I said, opended the browser and had a look at the requests that need to be performed to download a single paper.

When submitting the doi the following request is sent.

image

The http code 302 means that there is a redirection happening. If we want automate this process following redirects must be enabled.

image

The redirected response contains the paper. The url to the pdf file can be extracted using regex.

The initial request can be copied as curl command. This makes writing a script and bypassing any agent-checks much easier.

image

We both started Visual Studio Code and connected via the live coding plugin. My brother sent me a excerpt of the doi list:

doi-list.txt

10.3390/ijms21062239
10.1007/s12094-019-02276-8
10.31782/IJCRR.2019.11242
10.1016/j.chom.2019.05.007
10.3390/v11070638
10.1128/IAI.00733-18
10.1186/s12861-019-0183-y
10.1155/2019/8472712
10.1002/stem.2852
10.3390/genes9040176
10.1016/j.neulet.2018.01.040
10.1093/molehr/gax070
10.15252/emmm.201708213
10.1007/s40139-017-0137-7
10.1111/cas.13155
10.4103/0366-6999.191782
10.1126/science.aaf5211

And together we built this bash simple script:

scihub-download.sh

urlencode() {
    # urlencode <string>
    old_lc_collate=$LC_COLLATE
    LC_COLLATE=C

    local length="${#1}"
    for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
        case $c in
            [a-zA-Z0-9.~_-]) printf "$c" ;;
            *) printf '%%%02X' "'$c" ;;
        esac
    done

    LC_COLLATE=$old_lc_collate
}

readarray -t list <doi-list.txt

for doi in "${list[@]}"
do
    echo "Download for doi: $(urlencode $doi)"
    link=$(curl -s -L 'https://sci-hub.tw/' --compressed \
        -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0' \
        -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' \
        -H 'Accept-Language: en-US,en;q=0.5' \
        -H 'Content-Type: application/x-www-form-urlencoded' \
        -H 'Origin: https://sci-hub.tw' \
        -H 'DNT: 1' \
        -H 'Connection: keep-alive' \
        -H 'Referer: https://sci-hub.tw/' \
        -H 'Cookie: __ddg1=SFEVzNPdQpdmIWBwzsBq; session=45c4aaad919298b2eb754b6dd84ceb2d; refresh=1588795770.5886; __ddg2=6iYsE2844PoxLmj7' \
        -H 'Upgrade-Insecure-Requests: 1' \
        -H 'Pragma: no-cache' \
        -H 'Cache-Control: no-cache' \
        -H 'TE: Trailers' \
        --data "sci-hub-plugin-check=&request=$(urlencode $doi)". | grep -oP  "(?<=//).+(?=#)")

    echo "Found link: $link"
    curl -s -L $link --output $(urlencode $doi).pdf
done

It reads the text file and sends a request to scihub for each entry. If the response contains a valid pdf link, the file is downloaded. The doi request parameter must be url encoded.

In the next step he will process the content of the papers and analyze them for specific

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市曼库,隨后出現(xiàn)的幾起案子区岗,更是在濱河造成了極大的恐慌,老刑警劉巖毁枯,帶你破解...
    沈念sama閱讀 206,013評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件慈缔,死亡現(xiàn)場離奇詭異,居然都是意外死亡种玛,警方通過查閱死者的電腦和手機(jī)藐鹤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,205評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赂韵,“玉大人娱节,你說我怎么就攤上這事〖朗荆” “怎么了肄满?”我有些...
    開封第一講書人閱讀 152,370評論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我稠歉,道長掰担,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,168評論 1 278
  • 正文 為了忘掉前任怒炸,我火速辦了婚禮带饱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘阅羹。我一直安慰自己勺疼,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,153評論 5 371
  • 文/花漫 我一把揭開白布捏鱼。 她就那樣靜靜地躺著执庐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪穷躁。 梳的紋絲不亂的頭發(fā)上耕肩,一...
    開封第一講書人閱讀 48,954評論 1 283
  • 那天,我揣著相機(jī)與錄音问潭,去河邊找鬼猿诸。 笑死,一個(gè)胖子當(dāng)著我的面吹牛狡忙,可吹牛的內(nèi)容都是我干的梳虽。 我是一名探鬼主播,決...
    沈念sama閱讀 38,271評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼灾茁,長吁一口氣:“原來是場噩夢啊……” “哼窜觉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起北专,我...
    開封第一講書人閱讀 36,916評論 0 259
  • 序言:老撾萬榮一對情侶失蹤禀挫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后拓颓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體语婴,經(jīng)...
    沈念sama閱讀 43,382評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,877評論 2 323
  • 正文 我和宋清朗相戀三年驶睦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了砰左。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,989評論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡场航,死狀恐怖缠导,靈堂內(nèi)的尸體忽然破棺而出铣揉,到底是詐尸還是另有隱情烙常,我是刑警寧澤,帶...
    沈念sama閱讀 33,624評論 4 322
  • 正文 年R本政府宣布宿崭,位于F島的核電站,受9級特大地震影響嫡意,放射性物質(zhì)發(fā)生泄漏举瑰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,209評論 3 307
  • 文/蒙蒙 一蔬螟、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧汽畴,春花似錦旧巾、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,199評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至罢坝,卻和暖如春廓握,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嘁酿。 一陣腳步聲響...
    開封第一講書人閱讀 31,418評論 1 260
  • 我被黑心中介騙來泰國打工隙券, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人闹司。 一個(gè)月前我還...
    沈念sama閱讀 45,401評論 2 352
  • 正文 我出身青樓娱仔,卻偏偏與公主長得像,于是被迫代替她去往敵國和親游桩。 傳聞我的和親對象是個(gè)殘疾皇子牲迫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,700評論 2 345