使用BS4解析HTML表(Parsing HTML Tables with BS4)

我一直在尝试从这个站点抓取数据的不同方法( http://nflcombineresults.com/nflcombinedata.php?year=1999&pos=WR&college= ),似乎无法让它们中的任何一个工作。 我尝试过使用指数,但似乎无法使其发挥作用。 我想我此刻已经尝试了太多东西,所以如果有人能指出我正确的方向,我会非常感激。

我想提取所有信息并将其导出到.csv文件,但此时我只是想获取打印的名称和位置以开始使用。

这是我的代码:

import urllib2
from bs4 import BeautifulSoup
import re

url = ('http://nflcombineresults.com/nflcombinedata.php?year=1999&pos=&college=')

page = urllib2.urlopen(url).read()

soup = BeautifulSoup(page)
table = soup.find('table')

for row in table.findAll('tr')[0:]:
    col = row.findAll('tr')
    name = col[1].string
    position = col[3].string
    player = (name, position)
    print "|".join(player)

这是我得到的错误:第14行,名称= col [1] .string IndexError:列表索引超出范围。

--UPDATE--

好的,我已经取得了一些进展。 它现在允许我从头到尾,但它需要知道表中有多少行。 我怎么能直到最后才通过它们? 更新的代码:

import urllib2
from bs4 import BeautifulSoup
import re

url = ('http://nflcombineresults.com/nflcombinedata.php?year=1999&pos=&college=')

page = urllib2.urlopen(url).read()

soup = BeautifulSoup(page)
table = soup.find('table')


for row in table.findAll('tr')[1:250]:
    col = row.findAll('td')
    name = col[1].getText()
    position = col[3].getText()
    player = (name, position)
    print "|".join(player)

I've been trying different methods of scraping data from this site (http://nflcombineresults.com/nflcombinedata.php?year=1999&pos=WR&college=) and can't seem to get any of them to work. I've tried playing with the indices given, but can't seem to make it work. I think I've tried too many things at this point,so if someone could point me in the right direction I would really appreciate it.

I would like to pull all of the information and export it to a .csv file, but at this point I'm just trying to get the name and position to print to get started.

Here's my code:

import urllib2
from bs4 import BeautifulSoup
import re

url = ('http://nflcombineresults.com/nflcombinedata.php?year=1999&pos=&college=')

page = urllib2.urlopen(url).read()

soup = BeautifulSoup(page)
table = soup.find('table')

for row in table.findAll('tr')[0:]:
    col = row.findAll('tr')
    name = col[1].string
    position = col[3].string
    player = (name, position)
    print "|".join(player)

Here's the error I'm getting: line 14, in name = col[1].string IndexError: list index out of range.

--UPDATE--

Ok, I've made a little progress. It now allows me to go from start to finish, but it requires knowing how many rows are in the table. How would I get it to just go through them until the end? Updated Code:

import urllib2
from bs4 import BeautifulSoup
import re

url = ('http://nflcombineresults.com/nflcombinedata.php?year=1999&pos=&college=')

page = urllib2.urlopen(url).read()

soup = BeautifulSoup(page)
table = soup.find('table')


for row in table.findAll('tr')[1:250]:
    col = row.findAll('td')
    name = col[1].getText()
    position = col[3].getText()
    player = (name, position)
    print "|".join(player)

原文:https://stackoverflow.com/questions/22078620
2022-06-27 10:06

满意答案

在Audacity中,您必须选中首选项的“ 导入/导出”部分中的 “使用自定义混合”单选按钮。 这将允许您导出多声道文件,并手动将曲目分配给频道。

除此之外,普通的旧.wav可以正常工作。

但您也可以使用SoX以更自动化的方式创建文件。

手动,您可以将五个不同的文件组合(或“合并”,如文档中所述)五个不同的文件,如下所示:

sox -M chan1.wav chan2.wav chan3.wav chan4.wav chan5.wav multi.wav

为了自动化这个过程,我整理了一个简短的Bash例程,用于生成具有交错测试音的多声道文件:

NUM=5    # Number of channels
LEN=2    # Length of each test tone, in seconds
OVL=0.5  # Overlap between test tones, in seconds

# A one-channel base file containing simple white noise.
# faded at both end with a quarter wave envelope to ensure 
# smooth equal power transitions
sox -n -b 24 -c 1 out.wav synth $LEN whitenoise fade q $OVL -0 $OVL

# Instead of white noise you can for example make a 1kHz tone
# like this:
# sox -n -b 24 -c 1 out.wav synth $LEN sine 1k fade q $OVL -0 $OVL

# Or a sweep from 10Hz to 10kHz like this:
# sox -n -b 24 -c 1 out.wav synth $LEN sine 10-10k fade q $OVL -0 $OVL

# Produces a sequence of the number of seconds each channel
# shall be padded with
SEQ=$(for ((i=1; i<=NUM; i++))
do 
  echo "$i 1 - [$LEN $OVL -]x * p" | dc  # reverse-Polish arithmetic
done)

echo $SEQ

# Padding the base file to various degrees and saving them separately
for j in $SEQ
do 
  sox -c 1 out.wav outpad${j}.wav pad $j
done

# Finding the just-produced individual files
FIL=$(ls | grep ^outpad)

# Merging the individual files into a single multi-channel file
sox -M $FIL multi.wav

rm $FIL  # removing the individual files

# Producing a multi-channel waveform plot
ffmpeg -i multi.wav -y -filter_complex "showwavespic=s=2400x900:split_channels=1" -frames:v 1 waveform.png

# displaying the waveform plot
open waveform.png

如波形图清晰显示,结果由一个包含五个通道的文件组成,每个通道具有相同的内容,只是在一段时间内移动:

具有交错和交叉褪色测试音调的多声道音频文件

更多关于使用dc反向波兰算法: http//wiki.bash-hackers.org/howto/calculate-dc

有关使用ffmpeg显示波形的更多信息: https//trac.ffmpeg.org/wiki/Waveform


In Audacity you have to check the 'Use custom mix' radio button in the Import/Export section of the preferences. This will let you export multi-channel files, and manually assign tracks to channels.

Other than that, plain old .wav works fine for this.

But you can also use SoX to create the files in a more automated manner.

Manually you can combine (or 'merge' as it's referred to in the documentation) five distinct files into a single five-channel file like this:

sox -M chan1.wav chan2.wav chan3.wav chan4.wav chan5.wav multi.wav

To automate the process I put together a short Bash routine for producing a multichannel file with staggered test tones:

NUM=5    # Number of channels
LEN=2    # Length of each test tone, in seconds
OVL=0.5  # Overlap between test tones, in seconds

# A one-channel base file containing simple white noise.
# faded at both end with a quarter wave envelope to ensure 
# smooth equal power transitions
sox -n -b 24 -c 1 out.wav synth $LEN whitenoise fade q $OVL -0 $OVL

# Instead of white noise you can for example make a 1kHz tone
# like this:
# sox -n -b 24 -c 1 out.wav synth $LEN sine 1k fade q $OVL -0 $OVL

# Or a sweep from 10Hz to 10kHz like this:
# sox -n -b 24 -c 1 out.wav synth $LEN sine 10-10k fade q $OVL -0 $OVL

# Produces a sequence of the number of seconds each channel
# shall be padded with
SEQ=$(for ((i=1; i<=NUM; i++))
do 
  echo "$i 1 - [$LEN $OVL -]x * p" | dc  # reverse-Polish arithmetic
done)

echo $SEQ

# Padding the base file to various degrees and saving them separately
for j in $SEQ
do 
  sox -c 1 out.wav outpad${j}.wav pad $j
done

# Finding the just-produced individual files
FIL=$(ls | grep ^outpad)

# Merging the individual files into a single multi-channel file
sox -M $FIL multi.wav

rm $FIL  # removing the individual files

# Producing a multi-channel waveform plot
ffmpeg -i multi.wav -y -filter_complex "showwavespic=s=2400x900:split_channels=1" -frames:v 1 waveform.png

# displaying the waveform plot
open waveform.png

As the waveform plot clearly shows, the result consists of a file with five channels, each with the same content, just moved about some in time:

multichannel audio file with staggered and cross-faded test tones

More on reverse-Polish arithmetic using dc: http://wiki.bash-hackers.org/howto/calculate-dc

More on displaying waveforms using ffmpeg: https://trac.ffmpeg.org/wiki/Waveform

相关问答

更多

FFMpeg如何从wav / .w64中提取单个音频通道,并使用轨道标记插入.mxf(FFMpeg How to extract individual audio channels from wav/.w64 and insert in .mxf with track tags)

你的映射搞砸了 错误消息指出“必须只有一个视频流,它必须是第一个”。 MXF很挑剔,因此您必须首先映射视频,因为映射顺序将决定输出中的流顺序。 其次,您尝试使用-map进行音频通道选择,但它不能像这样工作:您必须添加-map_channel或平移音频过滤器。 -map_channel ffmpeg -i "D:\Media\AUDIO_0.W64" -i "D:\media\NO_AUDIO.mxf" -map 1:v -map 0:a -map 0:a -map 0:a -map_channel...

左右音频通道正在交换(Left and Right audio channels are exchanging)

存储在wav文件中的立体声PCM是LR格式。 'L'代表左声道样本,'R'代表右声道样本。 我想你在检索或存储PCM时有一个错误。 也许有时你会从缓冲区中的正确(正确)位置开始,有时你会从第二个样本开始。 没有其他信息很难分辨。 stereo PCM stored in a wav file is in an LR format. 'L' stands for left channel sample and 'R' for right channel sample. I guess you hav...

在ffmpeg中平移所有音频频道中心?(Panning all audio channels center in ffmpeg?)

音频滤波器在FFMPEG上具有不同的sintax。 您可以平移音频通道而不会产生混音。 使用你的例子: ffmpeg -f lavfi -i "amovie=inMovie.mov,pan=stereo: c0=c0+c1: c1=c0+c1" -i inMovie.mov -map 0:0 -map 1:0 -vcodec libx264 -vpre medium -b 320k -pass 1 -s 374x210 -threads 0 -acodec libfaac -ab 64k outM...

1个文件中的5个独立音频通道(5 individual audio channels in 1 file)

在Audacity中,您必须选中首选项的“ 导入/导出”部分中的 “使用自定义混合”单选按钮。 这将允许您导出多声道文件,并手动将曲目分配给频道。 除此之外,普通的旧.wav可以正常工作。 但您也可以使用SoX以更自动化的方式创建文件。 手动,您可以将五个不同的文件组合(或“合并”,如文档中所述)五个不同的文件,如下所示: sox -M chan1.wav chan2.wav chan3.wav chan4.wav chan5.wav multi.wav 为了自动化这个过程,我整理了一个简短的B...

如何混合两个音频通道?(how to mix two audio channels?)

混合只是两个信号的加权加法。 因此,如果您希望它们在一个单声道信号中相等,则将两个信号降低2倍并添加它们。 如果要将它们放置在立体声空间中,请在左右声道上使用不同的加权。 例如,信道1的0.6和左声道上的信号2的0.4,反之亦然,右声道将完成。 为了获得更好的结果,需要稍微移档,但这取决于您的需求。 Mixing is just a weighted addition of both signals. So if you want them to be equal in one mono sign...

分离然后加入.wav立体声通道的混乱音频(Choppy audio from separating and then joining .wav stereo channels)

因为它经常发生,我睡在它上,并在第二天用解决方案醒来。 问题出在combineChannels函数中。 以下是工作代码: def combineChannels(self, left, right): stereoData = left for i in range(0, self.CHUNK): index = i*2+1 stereoData = np.insert(stereoData, index, right[i:(i+1)]) ...

如果有多个通道,则合并,然后从音频文件中获取采样长度并将其保存到s3(If multiple channels, merge then take sample length from audio file and save it to s3)

Transloadit支持注入ffmpeg参数。 所以你可以很容易地将你的参数添加到ffmpeg对象中,如下所示。 ffmpeg:{ ab: '128k', "ss": "0", "t": "30" } 如果您对support@transloadit.com有其他疑问,请告诉我们 Transloadit supports injection of ffmpeg params. So you can easily add your params into ffmpeg object as fol...

如何使用外部音频接口访问具有核心音频的单个通道(How to access individual channels with core audio from an external audio interface)

虽然您的代码对于它似乎管理的任务看起来过于复杂,但我会尝试回答您的问题: 在回调中检索样本数据的概念没有任何问题。 如果处理多声道音频设备,那将是不够的。 设备有多少个通道,通过AudioStreamBasicDescription查询给定设备的通道布局,格式等。 此属性用于初始化您的处理链的其余部分。 您在初始化时分配音频缓冲区,或让程序为您执行此操作(请阅读文档)。 如果你觉得使用额外的缓冲区更容易复制到数据处理和DSP,你可以在回调中管理它(简化代码): Float32 buf[stream...

将多个音频输出到各个声卡通道(Output multiple audio to individual sound card channels)

您可以使用MultiplexingSampleProvider或MultiplexingWaveProvider从其中一个立体声对中播放两个独立的单声道流。 如果您想将整个声卡视为单个设备,那么我发现AsioOut往往是唯一的选择,您可以再次使用多路复用提供程序将各个NAudio波流路由到不同的设备输出。 You can play two independent mono streams out of one of the stereo pairs using MultiplexingSample...

相关文章

更多

英特尔推出Hadoop免费版本 布局BS时代

在近期于北京举办的2012年Hadoop与大数据技术大会上,Intel公司正式在中国宣布推出Intel ...

Cocos-html5 初识

Cocos html5 项目环境的搭建。按照官网的文档搭建。(cocos html5 v2.2.3、w ...

nodejs与html代码分离

有没有什么办法不用框架就可以让html和js代码分离,每写一句html都要用引号和加号忒麻烦. 怎样 ...

HTML转义标签

空格 空格是 HTML 中最普通的字符实体。 通常情况下,HTML 会裁掉文档中的空格。假如你在文档 ...

HTML 排版标记

HTML 排版标记包括换行、hr(横线)、p(段落)、pre等 示例: &lt;html&gt ...

在HTML中使用javascript

1&gt;页面中直接在script标签中书写 &lt;html&gt;&lt;head&gt;&lt; ...

HTML meta标签

meta标签 :属性标签 . 网页元标签 &lt;meta http-equiv=&quot;Con ...

在html使用CSS的方式

结合方式01: 在标签上加入style属性. 属性的值就是css代码. 示例: & ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本