2016-07-28 2 views
0

this website에서 필요한 번호를 얻는 코드를 작성했지만 다음에해야할 일을 모릅니다.파이썬과 아름다운 스프를 사용하여 웹 사이트의 테이블 데이터를 내 웹 사이트의 테이블에 삽입하십시오.

하단의 표에서 숫자를 가져옵니다. 분만 중 분만, 출생 체중, 이유중인 체중, 육체 체중, 우유 및 총 모성.

#!/usr/bin/python 
import urllib2 
from bs4 import BeautifulSoup 
import pyperclip 

def getPageData(url): 

    if not ('abri.une.edu.au' in url): 
     return -1 
    webpage = urllib2.urlopen(url).read() 
    soup = BeautifulSoup(webpage, "html.parser") 

    # This finds the epd tree and saves it as a searchable list 
    pedTreeTable = soup.find('table', {'class':'TablesEBVBox'}) 

    # This puts all of the epds into a list. 
    # it looks for anything in pedTreeTable with an td tag. 
    pageData = pedTreeTable.findAll('td') 
    pageData.pop(7) 
    return pageData 

def createPedigree(animalPageData): 
    ''' make animalPageData much more useful. Strip the text out and put it in a dict.''' 
    animals = [] 
    for animal in animalPageData: 
     animals.append(animal.text) 

    prettyPedigree = { 
    'calving_ease' : animals[18], 
    'birth_weight' : animals[19], 
    'wean_weight' : animals[20], 
    'year_weight' : animals[21], 
    'milk' : animals[22], 
    'total_mat' : animals[23] 
    }  

    for animalKey in prettyPedigree: 
     if animalKey != 'year_weight' and animalKey != 'dam': 
      prettyPedigree[animalKey] = stripRegNumber(prettyPedigree[animalKey]) 
    return prettyPedigree 

def stripRegNumber(animal): 
    '''returns the animal with its registration number stripped''' 
    lAnimal = animal.split() 
    strippedAnimal = "" 
    for word in lAnimal: 
     if not word.isdigit(): 
      strippedAnimal += word + " " 
    return strippedAnimal 

def prettify(pedigree): 
    ''' Takes the pedigree and prints it out in a usable format ''' 
    s = '' 

    pedString = "" 

    # this is also ugly, but it was the only way I found to format with a variable 
    cFormat = '{{:^{}}}' 
    rFormat = '{{:>{}}}' 

    #row 1 of string 
    s += rFormat.format(len(pedigree['calving_ease'])).format(
          pedigree['calving_ease']) + '\n' 

    #row 2 of string 
    s += rFormat.format(len(pedigree['birth_weight'])).format(
          pedigree['birth_weight']) + '\n' 

    #row 3 of string 
    s += rFormat.format(len(pedigree['wean_weight'])).format(
          pedigree['wean_weight']) + '\n' 

    #row 4 of string 
    s += rFormat.format(len(pedigree['year_weight'])).format(
          pedigree['year_weight']) + '\n' 

    #row 4 of string 
    s += rFormat.format(len(pedigree['milk'])).format(
          pedigree['milk']) + '\n' 

    #row 5 of string 
    s += rFormat.format(len(pedigree['total_mat'])).format(
          pedigree['total_mat']) + '\n' 


    return s 

if __name__ == '__main__': 
    while True: 
     url = raw_input('Input a url you want to use to make life easier: \n') 
     pageData = getPageData(url) 
     s = prettify(createPedigree(pageData)) 
     pyperclip.copy(s) 
     if len(s) > 0: 
      print 'the easy string has been copied to your clipboard' 

나는 쉽게 복사하여 붙여 넣기 위해이 코드를 사용했습니다. URL을 삽입하기 만하면 클립 보드에 번호가 저장됩니다.

이제이 코드를 내 웹 사이트에서 사용하고 싶습니다. 내 HTML 코드에 URL을 삽입 할 수있게하려면 테이블의 내 페이지에이 번호가 표시됩니다.

  1. 어떻게 웹 사이트에 파이썬 코드를 사용합니까 다음과 같이

    내 질문

    은?
  2. 수집 된 데이터를 HTML이있는 테이블에 어떻게 삽입합니까?
+0

사이트에서 어떤 프레임 워크를 사용하고 있습니까? –

+0

여러 웹 사이트에서이 기능을 구현하고 싶습니다. 또한 다른 유형의 프레임 워크를 완전히 이해하지 못합니다. 나는 아주 아마추어이고 나의 웹 사이트도 그렇다. 이 정보가 도움이되는지 모르겠지만 HTML, CSS 및 자바 스크립트를 사용합니다. –

답변

0

Django과 같은 것을 사용하려는 것 같습니다. 학습 곡선이 조금 가파르지만 (물론) python을 지원합니다.