【发布时间】: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