Home > Net >  capture updated exchange rate by using python
capture updated exchange rate by using python

Time:03-12

I am trying to capture exchange rate from hexun.com,

from bs4 import BeautifulSoup
import requests
urls = 'http://so.hexun.com/default.do?type=forex&key=iskcny'
html = requests.get(urls)
soup = bs(html.text, "html.parser")

s1 = soup.find('span', attrs={"id": "ISKCNYforexprice"})
s2 = soup.find_all("span")
result = [span.get_text() for span in s2]
print(result)

below is error message:

Traceback (most recent call last):
  File "Z:\cur\isk_Rate.py", line 4, in <module>
    html = requests.url(urls).read()
AttributeError: module 'requests' has no attribute 'url'

CodePudding user response:

Replace

html = requests.url(urls).read()
soup = BeautifulSoup(html, 'html.parser')

with

html = requests.get(urls)
soup = bs(html.text, "html.parser")

CodePudding user response:

The data that the url contain which is dynamic meaning data is generated by JavaScript and BeautifulSoup can't render javaSceipt.So, You need automation tool something like selenium with BeautifulSoup. Please just run the code.

import time
from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

url = 'http://so.hexun.com/default.do?type=forex&key=iskcny'

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
time.sleep(8)
driver.get(url)
time.sleep(10)


soup = BeautifulSoup(driver.page_source, "html.parser")

s1 = soup.find('span', attrs={"id": "ISKCNYforexprice"})
s2 = soup.find_all("span")
result = [span.get_text() for span in s2]
print(result)

Output:

['', '登录', '', '注册', '(1)', '0.4768', '0.00%']
  • Related