【问题标题】:Insert table data from website into table on my own website using Python and Beautiful Soup使用 Python 和 Beautiful Soup 将网站中的表格数据插入我自己网站上的表格中
【发布时间】:2016-12-03 07:28:08
【问题描述】:

我写了一些代码,从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. 网站上的python代码如何使用?
  2. 如何将收集到的数据插入到 HTML 表格中?

【问题讨论】:

  • 您在您的网站上使用什么框架?
  • 我希望在多个网站上实现这一点。我也不完全理解不同类型的框架。我很业余,我的网站也是如此。我不知道这些信息是否有帮助,但我使用 html、css 和 javascript

标签: python html web-scraping beautifulsoup html-parsing


【解决方案1】:

听起来您想使用Django 之类的东西。虽然学习曲线有点陡峭,但值得并且它(当然)支持python。

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多