簡體   English   中英

登錄網站以使用python進行抓取

[英]login to website for scraping with python

我需要從一個網站獲得遺傳途徑的鏈接。 首先,我需要登錄,但遇到麻煩。 我在抓取方面經驗很少,因此會非常感謝您提供任何指針或一般性的“如何”信息以及准確的答案。

import requests
from bs4 import BeautifulSoup
URL = 'http://www.broadinstitute.org/gsea/msigdb/genesets.jsp?collection=CP:BIOCARTA'
session1 = requests.Session()
params = {'login':'my_email'}
session2 = session1.post(URL, data=params)

pathways_links = []

for link in soup.find('div', attrs={'id':'wrapper'}).find(
    'div', attrs={'id':'contentwrapper'}).find(
        'div', attrs={'id':'content_navs'}).find(
            'table', attrs={'id':'geneSetTable'}).find('a')['href']:
    pathways_links.append(link)
    print link

不幸的是,它似乎沒有登錄。我得到:

'div', attrs={'id':'content_navs'}).find(
 AttributeError: 'NoneType' object has no attribute 'find'

如果我要求它在'content_navs'div之前打印鏈接,則會得到:

<div id="content_full">
<h1>Login to GSEA/MSigDB</h1>
<h2>Login</h2>
<a href="register.jsp"></a>Click here</div>

任何解決方案將不勝感激。 謝謝。

您需要先登錄http://www.broadinstitute.org/gsea/login.jsp ,然后再轉到其他位置。

第一步是創建一個會話對象。 它將保留cookie和其他會話詳細信息。 接下來,您需要登錄,然后最終將內容傳遞給BeautifulSoup:

s = requests.Session()
data = {'j_username': 'you@email.com'}
s.post('http://www.broadinstitute.org/gsea/login.jsp', data=data)
r = s.get('http://www.broadinstitute.org/gsea/msigdb/genesets.jsp?collection=CP:BIOCARTA')
soup = BeautifulSoup(r.content)

# the rest of your code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM