在一个混乱的网站上使用漂亮的汤进行Python Web抓取

在一个混乱的网站上使用漂亮的汤进行Python Web抓取,python,web-scraping,screen-scraping,beautifulsoup,Python,Web Scraping,Screen Scraping,Beautifulsoup,我想从以下三个数据点中提取:%verified、FAR的数值和POD的数值。我试图在BeautifulSoup中这样做,但我不擅长站点遍历,所以我无法描述这些元素的位置 做这件事最简单的方法是什么 如果尚未安装,请安装Firefox并使用它检查页面的html源代码 结合使用urllib和来处理html检索和解析。下面是一个简短的例子: import urllib from BeautifulSoup import BeautifulSoup url = 'http://mesonet.agro

我想从以下三个数据点中提取:%verified、FAR的数值和POD的数值。我试图在BeautifulSoup中这样做,但我不擅长站点遍历,所以我无法描述这些元素的位置


做这件事最简单的方法是什么

如果尚未安装,请安装Firefox并使用它检查页面的html源代码

结合使用
urllib
和来处理html检索和解析。下面是一个简短的例子:

import urllib
from BeautifulSoup import BeautifulSoup

url = 'http://mesonet.agron.iastate.edu/cow/?syear=2009&smonth=9&sday=12&shour=12&eyear=2012&emonth=9&eday=12&ehour=12&wfo=ABQ&wtype[]=TO&hail=1.00&lsrbuffer=15&ltype[]=T&wind=58'
fp = urllib.urlopen(url).read()
soup = BeautifulSoup(fp)

print soup

从这里开始,我提供的链接将为您提供一个良好的开端,让您了解如何检索您感兴趣的元素。

如果您还没有,请安装Firefox并使用它检查页面的html源代码

结合使用
urllib
和来处理html检索和解析。下面是一个简短的例子:

import urllib
from BeautifulSoup import BeautifulSoup

url = 'http://mesonet.agron.iastate.edu/cow/?syear=2009&smonth=9&sday=12&shour=12&eyear=2012&emonth=9&eday=12&ehour=12&wfo=ABQ&wtype[]=TO&hail=1.00&lsrbuffer=15&ltype[]=T&wind=58'
fp = urllib.urlopen(url).read()
soup = BeautifulSoup(fp)

print soup

从这里开始,我提供的链接将为您提供一个很好的开始,让您了解如何检索您感兴趣的元素。

正如1Guy所说,您需要分析源页面结构。在这种情况下,你很幸运。。。使用
,您要查看的数字会以红色突出显示

这将实现以下目的:

>>> import urllib2
>>> import lxml.html
>>> url = ... # put your URL here
>>> html = urllib2.urlopen(url)
>>> soup = lxml.html.soupparser.fromstring(html)
>>> elements = soup.xpath('//th/span')
>>> print float(elements[0].text) # FAR
0.67
>>> print float(elements[1].text) # POD
0.58

注意
lxml.html.soupparser
相当于
BeautifulSoup
解析器(我现在不需要它)。

正如1Guy所说,您需要分析源页面结构。在这种情况下,你很幸运。。。使用
,您要查看的数字会以红色突出显示

这将实现以下目的:

>>> import urllib2
>>> import lxml.html
>>> url = ... # put your URL here
>>> html = urllib2.urlopen(url)
>>> soup = lxml.html.soupparser.fromstring(html)
>>> elements = soup.xpath('//th/span')
>>> print float(elements[0].text) # FAR
0.67
>>> print float(elements[1].text) # POD
0.58

注意
lxml.html.soupparser
相当于
BeautifulSoup
解析器(我现在不需要它)。

我自己解决了这个问题——我使用了一种类似于isedev的策略,但我希望能找到一种更好的方法来获取“验证”数据:

import urllib2
from bs4 import BeautifulSoup

wfo = list()

def main():
    wfo = [i.strip() for i in open('C:\Python27\wfo.txt') if i[:-1]]
    soup = BeautifulSoup(urllib2.urlopen('http://mesonet.agron.iastate.edu/cow/?syear=2009&smonth=9&sday=12&shour=12&eyear=2012&emonth=9&eday=12&ehour=12&wfo=ABQ&wtype%5B%5D=TO&hail=1.00&lsrbuffer=15&ltype%5B%5D=T&wind=58').read())
    elements = soup.find_all("span")
    find_verify = soup.find_all('th')

    far= float(elements[1].text)
    pod= float(elements[2].text)
    verified = (find_verify[13].text[:-1])

我最终自己解决了这个问题——我使用了一种类似于isedev的策略,但我希望能找到一种更好的方法来获取“验证”数据:

import urllib2
from bs4 import BeautifulSoup

wfo = list()

def main():
    wfo = [i.strip() for i in open('C:\Python27\wfo.txt') if i[:-1]]
    soup = BeautifulSoup(urllib2.urlopen('http://mesonet.agron.iastate.edu/cow/?syear=2009&smonth=9&sday=12&shour=12&eyear=2012&emonth=9&eday=12&ehour=12&wfo=ABQ&wtype%5B%5D=TO&hail=1.00&lsrbuffer=15&ltype%5B%5D=T&wind=58').read())
    elements = soup.find_all("span")
    find_verify = soup.find_all('th')

    far= float(elements[1].text)
    pod= float(elements[2].text)
    verified = (find_verify[13].text[:-1])