尽管网站有不同的结果,但在每个循环中使用 urllib 和 bs4 从地址获得相同的结果
getting same result in every loop with urllib and bs4 from an address although site has different results
from urllib import request
from bs4 import BeautifulSoup
import time
while True:
url = "https://coinmarketcap.com/currencies/bitcoin/markets/"
response = request.urlopen(url)
soup = BeautifulSoup(response, 'html.parser')
result = (soup.body.div.div.find("div", {"class":"container cmc-main-section"})
.find("div", {"class":"cmc-main-section__content"}).div.div.div
.find("div", {"class":"f6l7tu-0 jGlJVl cmc-details-panel-price"}).span.span)
b=[]
b = list(filter(None, result))
print(b[0])
time.sleep(10)
这是我的代码
每当我 运行 程序没有错误但每次它打印相同的值
一开始:它有点咄咄逼人;)你应该在循环中缩进你的 time.sleep(10)
。
这是怎么回事?
在您请求网站的那一刻,您会得到一个初始内容,其中还包括 price
.
的“chached”值
稍后网站会动态更新内容,因此您必须等待该更新。
你可以做的是使用硒:
from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep
url = "https://coinmarketcap.com/currencies/bitcoin/markets/"
driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')
while True:
driver.get(url)
#driver.implicitly_wait(10)
sleep(10)
soup = BeautifulSoup(driver.page_source,"lxml")
result = soup.find("div", {"class":"f6l7tu-0 jGlJVl cmc-details-panel-price"}).span.span.text
print(result)
driver.close()
输出
.391,31
.396,88
.392,99
.381,44
.373,13
.373,13
.359,99
.359,99
.355,07
.327,24
.330,22
.375,47
.341,14
.390,14
.345,10
from urllib import request
from bs4 import BeautifulSoup
import time
while True:
url = "https://coinmarketcap.com/currencies/bitcoin/markets/"
response = request.urlopen(url)
soup = BeautifulSoup(response, 'html.parser')
result = (soup.body.div.div.find("div", {"class":"container cmc-main-section"})
.find("div", {"class":"cmc-main-section__content"}).div.div.div
.find("div", {"class":"f6l7tu-0 jGlJVl cmc-details-panel-price"}).span.span)
b=[]
b = list(filter(None, result))
print(b[0])
time.sleep(10)
这是我的代码 每当我 运行 程序没有错误但每次它打印相同的值
一开始:它有点咄咄逼人;)你应该在循环中缩进你的 time.sleep(10)
。
这是怎么回事?
在您请求网站的那一刻,您会得到一个初始内容,其中还包括 price
.
稍后网站会动态更新内容,因此您必须等待该更新。
你可以做的是使用硒:
from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep
url = "https://coinmarketcap.com/currencies/bitcoin/markets/"
driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')
while True:
driver.get(url)
#driver.implicitly_wait(10)
sleep(10)
soup = BeautifulSoup(driver.page_source,"lxml")
result = soup.find("div", {"class":"f6l7tu-0 jGlJVl cmc-details-panel-price"}).span.span.text
print(result)
driver.close()
输出
.391,31
.396,88
.392,99
.381,44
.373,13
.373,13
.359,99
.359,99
.355,07
.327,24
.330,22
.375,47
.341,14
.390,14
.345,10