Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么通过BeatifulSoup解析的字符串返回为None?_Python_Beautifulsoup - Fatal编程技术网

Python 为什么通过BeatifulSoup解析的字符串返回为None?

Python 为什么通过BeatifulSoup解析的字符串返回为None?,python,beautifulsoup,Python,Beautifulsoup,我找不到为什么我的两个变量在循环中定义时返回“None” 甚至打印“.span.text.find”(“France”)“也不起作用。我猜我的两个循环没有执行。我不知道为什么 我也怀疑问题是否来自汤。事实上,有时它是有效的。有时不是我不碰代码的地方 #IMPORT DES LIBRAIRIRES import bs4 import requests import re import pandas as pd from selenium import webdriver from selenium

我找不到为什么我的两个变量在循环中定义时返回“None”

甚至打印“.span.text.find”(“France”)“也不起作用。我猜我的两个循环没有执行。我不知道为什么

我也怀疑问题是否来自汤。事实上,有时它是有效的。有时不是我不碰代码的地方

#IMPORT DES LIBRAIRIRES
import bs4
import requests
import re
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import urllib.request
from bs4 import BeautifulSoup
from datetime import date


#VARIABLES                              
date_jour = date.today()
URL ="https://gisanddata.maps.arcgis.com/apps/opsdashboard/index.html#/bda7594740fd40299423467b48e9ecf6"

# Chrome session USING SELENIUM
#driver = webdriver.Chrome("C:/Users/33769/Desktop/chromedriver.exe")
#driver.get(URL)
#driver.implicitly_wait(100)
#soup=bs4.BeautifulSoup(driver.page_source,"html.parser")

#USING REQUESTS
req = requests.get('https://en.wikipedia.org/wiki/Python_(programming_language)')
soup=bs4.BeautifulSoup(req.text,"html.parser")


nb_infected = None
nb_deaths = None

#Infected Cases France
for span in soup.find_all('div',{'class':'flex-fluid list-item-content overflow-hidden'})[:12]:
    if span.text.find("France")!= -1:
        nb_infected = span.text.replace('\n\n','')
        nb_infected = nb_infected.replace('\xa0','')
        nb_infected = nb_infected.replace('France','')
        print("OKKKKKK")
    else:
        print("NOT OK")
    print(span.text.find('France')) # NOT EXECUTED... WHY ???


#Deaths France
for span in soup.find_all('div',{'class':'flex-fluid list-item-content overflow-hidden'})[420:480]:
    if span.text.find("France")!= -1:
        nb_deaths = span.text.replace('\n\n','')
        nb_deaths = nb_deaths.replace('\xa0','')
        nb_deaths = nb_deaths.replace('deaths\nFrance','')

print("To conclude, in France, there are " + str(nb_infected) + " infected individuals and " + str(nb_deaths) + " deaths" )
#ONLY THIS LAST FINE WORKS....

我得到了答案!我使用selenium从驱动程序创建页面,并使用time.sleep(5)确保页面正确显示!代码如下:

#IMPORT DES LIBRAIRIRES
import bs4
import requests
import re
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import urllib.request
import time
from bs4 import BeautifulSoup
from datetime import date


#VARIABLES                              
date_jour = date.today()
URL ="https://gisanddata.maps.arcgis.com/apps/opsdashboard/index.html#/bda7594740fd40299423467b48e9ecf6"


# Chrome session
driver = webdriver.Chrome("C:/Users/33769/Desktop/chromedriver.exe")
driver.get(URL)
driver.implicitly_wait(100)
time.sleep(5)

soup=bs4.BeautifulSoup(driver.page_source,"html.parser")

nb_infected = None
nb_deaths = None

#Infected Cases France
for span in soup.find_all('div',{'class':'flex-fluid list-item-content overflow-hidden'})[:12]:
    if span.text.find("France")!= -1:
        nb_infected = span.text.replace('\n\n','')
        nb_infected = nb_infected.replace('\xa0','')
        nb_infected = nb_infected.replace('France','')

#Deaths France
for span in soup.find_all('div',{'class':'flex-fluid list-item-content overflow-hidden'})[420:480]:
    if span.text.find("France")!= -1:
        nb_deaths = span.text.replace('\n\n','')
        nb_deaths = nb_deaths.replace('\xa0','')
        nb_deaths = nb_deaths.replace('deaths\nFrance','')

print("To conclude, in France, there are " + str(nb_infected) + " infected individuals and " + str(nb_deaths) + " deaths" )

有一个不同的端点不断提供更新的数据。您可以使用xhr来代替,并解析返回的json。似乎你可以使用这些数据,如果用于教育目的,我认为这是合格的

import requests

headers = {
   'Referer': 'https://www.arcgis.com/apps/opsdashboard/index.html',
    'User-Agent': 'Mozilla/5.0',
}

r = requests.get('https://services9.arcgis.com/N9p5hsImWXAccRNI/arcgis/rest/services/Nc2JKvYFoAEOFCG5JSI6/FeatureServer/2/query?f=json&where=Recovered%3C%3E0&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Recovered%20desc&resultOffset=0&resultRecordCount=250&cacheHint=true', headers=headers, verify=False)
data = r.json()['features']
france = [i['attributes'] for i in data if i['attributes']['Country_Region'] == 'France']
if france:
    print(france)
    print("To conclude, in France, there are " + f'{france[0]["Confirmed"]:,}' + " infected individuals and " + f'{france[0]["Deaths"]:,}' + " deaths" )

请不要以图像形式提供代码。而是以文本形式提供代码。谢谢,好了!抱歉…代码与imageGreat不匹配!谢谢!!