[英]Beautiful Soup Cleaning and Errors
我有以下代碼:
from bs4 import BeautifulSoup
import urllib2
from lxml import html
from lxml.etree import tostring
trees = urllib2.urlopen('http://aviationweather.gov/adds/metars/index? station_ids=KJFK&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&ch k_tafs=on&submit=Submit').read()
soup = BeautifulSoup(open(trees))
print soup.get_text()
item=soup.findAll(id="info")
print item
但是,當我在窗口上鍵入湯時,它給我一個錯誤,而當我的程序運行時,它給了我很長的html代碼,
等等。 任何幫助將是巨大的。
第一個問題在這部分中:
trees = urllib2.urlopen('http://aviationweather.gov/adds/metars/index?station_ids=KJFK&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&chk_tafs=on&submit=Submit').read()
soup = BeautifulSoup(open(trees))
trees
是一個類似文件的對象,不需要對其調用open()
進行修復:
soup = BeautifulSoup(trees, "html.parser")
我們還明確地將html.parser
設置為基礎解析器。
然后,您需要明確要從頁面中提取的內容。 這是獲取METAR text
值的示例代碼:
from bs4 import BeautifulSoup
import urllib2
trees = urllib2.urlopen('http://aviationweather.gov/adds/metars/index?station_ids=KJFK&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&chk_tafs=on&submit=Submit').read()
soup = BeautifulSoup(trees, "html.parser")
item = soup.find("strong", text="METAR text:").find_next("strong").get_text(strip=True).replace("\n", "")
print item
打印KJFK 220151Z 20016KT 10SM BKN250 24/21 A3007 RMK AO2 SLP183 T02440206
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.