【问题标题】:Scraping information from website using beautifullsoup wont work使用 beautifulsoup 从网站抓取信息不起作用
【发布时间】:2017-08-20 12:50:08
【问题描述】:

我一直在用美汤从网站http://slc.bioparadigms.org提取信息

但我只对疾病和 OMIM 编号感兴趣,因此对于列表中已有的每个 SLC 转运蛋白,我想提取这两个特征。问题是两者都与类 prt_col2 有关。所以,如果我搜索这个类,我会得到很多点击。我怎样才能只得到疾病?有时也没有与 SLC 转运蛋白相关的疾病,或者有时没有 OMIM 编号。我怎样才能提取信息?我在下面放了一些屏幕截图,向您展示它的外观。任何帮助将不胜感激!这是我在这里的第一篇文章,请原谅我的任何错误或遗漏信息。谢谢!

http://imgur.com/aTiGi84另一个是/L65HSym

理想情况下,输出将是例如:

转运体:SLC1A1

疾病:癫痫

OMIM:12345

编辑:我到目前为止的代码:

import os
import re
from bs4 import BeautifulSoup as BS
import requests
import sys
import time


def hasNumbers(inputString):                            #get transporter names which contain numbers
    return any(char.isdigit() for char in inputString)

def get_list(file):                                     #get a list of transporters
    transporter_list=[]
    lines = [line.rstrip('\n') for line in open(file)]
    for line in lines:
        if 'SLC' in line and hasNumbers(line) == True:
            get_SLC=line.split()
            if 'SLC' in get_SLC[0]:
                transporter_list.append(get_SLC[0])
    return transporter_list

def get_transporter_webinfo(transporter_list):
    output_Website=open("output_website.txt", "w")                                 # get the website content of all transporters
    for transporter in transporter_list:
        text = requests.get('http://slc.bioparadigms.org/protein?GeneName=' + transporter).text
        output_Website.write(text) #ouput from the SLC tables website         
        soup=BS(text, "lxml")
        disease = soup(text=re.compile('Disease'))
        characteristics=soup.find_all("span", class_="prt_col2")
        memo=soup.find_all("span", class_='expandable prt_col2')
        print(transporter,disease,characteristics[6],memo)

def convert(html_file):
    file2= open(html_file, 'r')
    clean_file= open('text_format_SLC','w')
    soup=BS(file2,'lxml')
    clean_file.write(soup.get_text())
    clean_file.close()

def main():

    start_time=time.time()
    os.chdir('/home/Programming/Fun stuff')
    sys.stdout= open("output_SLC.txt","w")
    SLC_list=get_list("SLC.txt")
    get_transporter_webinfo(SLC_list)                         #already have the website content so little redundant
    print("this took",time.time() - start_time, "seconds to run")
    convert("output_SLC.txt")
    sys.stdout.close() 

if __name__ == "__main__":
main()    

【问题讨论】:

    标签: python-3.x beautifulsoup web


    【解决方案1】:

    无意冒犯,我不想阅读您在问题中提出的如此大的代码。

    我会说它可以简化。

    您可以在SLCs = 的行中获取指向 SLC 的完整链接列表。下一行显示有多少个,然后一行显示最后一个链接包含的href 属性,例如。

    在每个 SLC 的页面中,我都会查找字符串“疾病”,如果存在,我会导航到附近的链接。我以类似的方式找到 OMIM。

    请注意,我只处理第一个 SLC。

    >>> import requests
    >>> import bs4
    >>> main_url = 'http://slc.bioparadigms.org/'
    >>> main_page = requests.get(main_url).content
    >>> main_soup = bs4.BeautifulSoup(main_page, 'lxml')
    >>> stem_url = 'http://slc.bioparadigms.org/protein?GeneName=SLC1A1'
    >>> SLCs = main_soup.select('td.slct.tbl_cell.tbl_col1 a')
    >>> len(SLCs)
    418
    >>> SLCs[-1].attrs['href']
    'protein?GeneName=SLC52A3'
    >>> stem_url = 'http://slc.bioparadigms.org/'
    >>> for SLC in SLCs:
    ...     SLC_page = requests.get(stem_url+SLC.attrs['href'], 'lxml').content
    ...     SLC_soup = bs4.BeautifulSoup(SLC_page, 'lxml')
    ...     disease = SLC_soup.find_all(string='Disease: ')
    ...     if disease:
    ...         disease = disease[0]
    ...         diseases = disease.findParent().findNextSibling().text.strip()
    ...     else:
    ...         diseases = 'No diseases'
    ...     OMIM = SLC_soup.find_all(string='OMIM:')
    ...     if OMIM:
    ...         OMIM = OMIM[0]
    ...         number = OMIM.findParent().findNextSibling().text.strip()
    ...     else:
    ...         OMIM = 'No OMIM'
    ...         number = -1
    ...     SLC.text, number, diseases
    ...     break
    ... 
    ('SLC1A1', '133550', "Huntington's disease, epilepsy, ischemia, Alzheimer's disease, Niemann-Pick disease, obsessive-compulsive disorder")
    

    【讨论】:

    • 如果此答案或任何其他答案符合您的要求,您应该将其标记为“已接受”。
    • 感谢您的帮助,非常感谢。我是编程新手,所以我必须学习很多东西。我想知道是否可以改进脚本,因为我也对类
    • 不客气。因为您在 SO 上提出了您的问题,所以我很自然地将其视为您试图解决的实际问题,而不是学习练习。人们确实在 SO(我愿意!)上学习,但我们专注于特定问题。如果您希望人们讨论您的代码,那么您可能应该转向 Code Golf。这里实际上不鼓励讨论。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2016-09-07
    • 2021-10-26
    • 2020-08-01
    • 2020-11-07
    相关资源
    最近更新 更多