我一直试图在网站"http://www.jps.go.cr/“上使用Beautifulsoup获取数据,但是当我获取数据时,标签之间的所有信息都丢失了。我可以确认数据在那里,检查网站,但是一旦我运行代码,它就不显示了。
代码如下:
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
url="http://www.jps.go.cr/productos/loteria-nacional"
req = Request(url,headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
soup=BeautifulSoup(c,"html.parser")
all=soup.find_all("div",{"class":"detail_ultimoSorteo loteria"})
print(all)
我将感谢任何帮助,因为我驾驶疯狂,其他网站的工作。
提前谢谢。
发布于 2018-12-04 23:27:56
页面加载速度很慢,需要使用selenium这样的方法,以便让您的内容有足够的时间可用。
from selenium import webdriver
url = 'http://www.jps.go.cr/'
driver = webdriver.Chrome()
driver.get(url)
print(driver.find_element_by_css_selector('.detail_ultimoSorteo.loteria').text)
输出:
发布于 2018-12-05 03:01:11
这将在你的情况下工作,只是为你张贴(我知道PhantomJS已经被弃用,你可以使用Chrome驱动程序)。
from bs4 import BeautifulSoup
from selenium import webdriver
url="http://www.jps.go.cr/productos/loteria-nacional"
browser = webdriver.PhantomJS()
browser.get(url)
html = browser.page_source
soup = BeautifulSoup(html, 'html.parser')
all=soup.find_all("div",{"class":"detail_ultimoSorteo"})
print(all)
输出输出
https://stackoverflow.com/questions/53616021
复制相似问题