나는 파이썬으로 웹 사이트에서 환율을 얻으 려하고 있습니다 : xe.com/currency/converter (다른 링크를 게시 할 수 없습니다, 죄송합니다 - 저는 'm at limit) 예를 들어 GBP와 USD 사이의 전환에 대해이 파일에서 요율을 얻을 수 있기를 원합니다. 따라서 URL을 검색하고 "http://www.xe.com/currencyconverter/convert/?Amount=1&From=GBP&To=USD"을 검색 한 다음 "1.56371 USD "(이 메시지를 쓰던 당시의 요율)를 사용하고 해당 값을 int와 같은 변수에 rate_usd와 같이 할당합니다. 지금은 BeautifulSoup 모듈과 urllib.request 모듈을 사용하고 URL ("http://www.xe.com/currencyconverter/convert/?Amount=1&From=GBP&To=USD")을 요청하고 BeautifulSoup을 사용하여 검색했습니다. 어떤 도움이 많이 주시면 감사하겠습니다Python : 웹 페이지 읽기 및 해당 페이지에서 텍스트 추출하기
import urllib.request
import bs4 from BeautifulSoup
def rates_fetcher(url):
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html)
# code to search through soup and fetch the converted value
# e.g. 1.56371
# How would I extract this value?
# I have inspected the page element and found the value I want to be in the class:
# <td width="47%" align="left" class="rightCol">1.56371
# I'm thinking about searching through the class: class="rightCol"
# and extracting the value that way, but how?
url1 = "http://www.xe.com/currencyconverter/convert/?Amount=1&From=GBP&To=USD"
rates_fetcher(url1)
, 이것을 읽을 시간이 걸렸 누구든지 감사 :이 순간, 나는 코딩이 단계에 있어요.
p.s. 미안 해요. 오타를 만들었다면, 나는 서둘러야 할 것 같습니다. s
BeautifulSoup에 익숙하지 않습니다. 정규식으로 간주 했습니까? re.findall (r '[0-9] + \. [0-9] + USD', html)은 작동해야하지만 테스트하지 않았습니다. –