美丽的汤,并要求不要从网页中提取文本-Python3

时间:2019-02-23 15:51:57

标签: python-3.x web-scraping beautifulsoup python-requests

我正在尝试获取当前的货币汇率。到目前为止,这是我的脚本:

<select name="client" onchange="this.form.submit()">
                <option value="" disabled selected>--client--</option>
                <?php
                    for ($i = 0; $i < count($idnameCli); $i++) { 
                        echo "<option value='$idnameCli[$i][1]'>$idnameCli[$i][0] $idnomCli[$i][1] </option>";
                    }
                ?>
</select>

但是,当我import requests from bs4 import BeautifulSoup # Collect and parse first page page = requests.get('https://www.xe.com/currencyconverter/convert/? Amount=1&From=GBP&To=USD') soup = BeautifulSoup(page.text, 'lxml') c_rate=soup.find(class_='converterresult-conversionTo') 提取元内容和链接href时,我一直在使用解析器,并且'html.parser'也会发生相同的问题

有什么想法吗?如果您尝试在页面上尝试提取1.30451的数字。

谢谢

1 个答案:

答案 0 :(得分:1)

硒可以帮助您做到这一点:

document.getElementById("moon").addEventListener("click", e => {
  localStorage.setItem('mode', (localStorage.getItem('mode') || 'dark') === 'dark' ? 'light' : 'dark'
  const elements = document.querySelectorAll("body, hr");
  elements.forEach(el => {
    if (localStorage.getItem('mode') === 'dark') {
      el.classList.add('dark') 
    } else {
      el.classList.remove('dark')
    }
  })
})

输出:

from selenium import webdriver
from bs4 import BeautifulSoup
url = 'https://www.xe.com/currencyconverter/convert/?%20Amount=1&From=GBP&To=USD'
browser=webdriver.Firefox()
browser.get(url)

soup=BeautifulSoup(browser.page_source)

rate = soup.select("a[href*=https://www.xe.com/currencycharts/?from=GBP&to=]")[1]
print(rate.text)

如果将1.30449 变量中的[1]更改为rate[2][3],则应将GPB的EUR,INR和AUD。