将动态表格单元格值导入python代码 [英] Import a dynamic table cell value into python code

查看:21
本文介绍了将动态表格单元格值导入python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import requests
from bs4 import BeautifulSoup

html = requests.get("https://www.haremaltin.com/canli-piyasalar/")

soup = BeautifulSoup(html.content)

atalira = soup.findall(?????)
for gold in atalira:
  price = gold.text
  print(price)

大家好,如果您转到页面 https://www.haremaltin.com/canli-piyasalar/ 在Altın Fiyatları"中你会看到Eski Ata".我想将这些值之一插入 ??????我的python代码的一部分,对我来说有点挑战.提前感谢您的时间.下面你可以看到我要插入的html代码和值

Hello everyone, if you go to page https://www.haremaltin.com/canli-piyasalar/ In "Altın Fiyatları" you will see "Eski Ata". I want to insert one of those values into ?????? part of my python code and it is a little bit challenging for me. Thank you for your time in advance. Below you can see that html codes and value that I want to insert

<span class="item end price"><span class="arrowWrapper"><!----> <!----></span>
                                    3.327
                                </span>

我找到了方法

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import time 

# pip install selenium
# apt-get update # to update ubuntu to correctly run apt install
# apt install chromium-chromedriver
# cp /usr/lib/chromium-browser/chromedriver /usr/bin
# use command above if you code on google colab

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

site = 'https://www.haremaltin.com/altin-fiyatlari'

wd = webdriver.Chrome('chromedriver', options=options)

wd.get(site)

time.sleep(5) # give chrome 5 seconds to load the page 

html = wd.page_source

df = pd.read_html(html)

gold = df[1][2][15] # table 1, column 2, row 15, the value I want
gold = int((float(gold))*1000)
# it was a float and even more float value that I got, something like 
# 3.252,000, so I tried to convert it into int so code above the 
# solution that I found

推荐答案

我不确定您是否可以这样做.最好的方法是获取您想要的此站点的 API,然后从那里开始.如果你不能得到它,找一个不同的网站.这是我不久前制作的示例代码.

I'm not sure you can do it this way. The best way is to get the API for this site you want and go from there. If you can't get it, find a different site. Here is a sample code I made a while back ago.

import re
import http.client

def gold_price():

    conn = http.client.HTTPSConnection("www.goldapi.io")
    payload = ''
    headers = {
    'x-access-token': 'goldapi-aq2kfluknfhfjz4-io',
    'Content-Type': 'application/json'
    }
    conn.request("GET", "/api/XAU/USD", payload, headers)
    res = conn.getresponse()
    data = res.read()
    data.decode("utf-8")
    txt = data.decode("utf-8")
    pattern = re.search(r'"price":\d\d\d\d',txt)

    # pattern = re.findall(r'\d\d\d\d',txt)
    print(pattern)
gold_price()

这篇关于将动态表格单元格值导入python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆