【问题标题】:Import a dynamic table cell value into python code将动态表格单元格值导入python代码
【发布时间】:2021-10-01 09:25:25
【问题描述】:
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 代码和值

<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

【问题讨论】:

    标签: python pandas web-scraping python-requests webdriver


    【解决方案1】:

    我不确定你是否可以这样做。最好的方法是获取您想要的该站点的 API,然后从那里开始。如果您无法获得它,请找到其他站点。这是我不久前制作的示例代码。

    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()
    

    【讨论】:

    • 感谢您的评论
    • 如果您觉得答案有用,请投票并将其标记为已选中。谢谢。
    • 我仍然想知道如何插入该值。一定有办法
    • 如果看网页源码,是找不到价格的。您将看到仪表板的容器,然后连接到 API。据我所知,唯一的方法是通过 API。
    猜你喜欢
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2019-12-25
    • 2020-05-24
    • 1970-01-01
    相关资源
    最近更新 更多