Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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
Python3:BeautifulSoup4未返回预期值_Python_Python 3.x_Web Scraping_Beautifulsoup - Fatal编程技术网

Python3:BeautifulSoup4未返回预期值

Python3:BeautifulSoup4未返回预期值,python,python-3.x,web-scraping,beautifulsoup,Python,Python 3.x,Web Scraping,Beautifulsoup,我目前正试图在python 3.6.4下使用BS4在网站上废弃一些数据,但返回的值并不是我所期望的: import requests from bs4 import BeautifulSoup link = "https://www.lacentrale.fr/listing?makesModelsCommercialNames=FERRARI&sortBy=priceAsc" request = requests.get(link) page = request.content so

我目前正试图在python 3.6.4下使用BS4在网站上废弃一些数据,但返回的值并不是我所期望的:

import requests
from bs4 import BeautifulSoup

link = "https://www.lacentrale.fr/listing?makesModelsCommercialNames=FERRARI&sortBy=priceAsc"
request = requests.get(link)
page = request.content
soup = BeautifulSoup(page, "html5lib")

price = soup.find("div", {"class" : "fieldPrice sizeC"}).text

print(price)
我应该得到“39900欧元”,但代码返回“47880欧元”

注意:即使没有JS,数据也应该是“39900欧元”


谢谢你的帮助

此页面上的编码声明错误,因此BeautifulSoup被告知使用错误的编码。您可以强制它使用正确的编码,如下所示:

import requests
from bs4 import BeautifulSoup

link = "https://www.lacentrale.fr/listing?makesModelsCommercialNames=FERRARI&sortBy=priceAsc"
request = requests.get(link)
page = request.content
soup = BeautifulSoup(page.decode('utf-8','ignore'), "html5lib")

price = soup.find("div", {"class": "fieldPrice sizeC"}).text

print(price)
产出:

49 070 €

使用
page.text

Ex:

import requests
from bs4 import BeautifulSoup

link = "https://www.lacentrale.fr/listing?makesModelsCommercialNames=FERRARI&sortBy=priceAsc"
request = requests.get(link)
page = request.text
soup = BeautifulSoup(page, "html.parser")

price = soup.find("div", {"class" : "fieldPrice sizeC"}).text

print(price)
  • .text
    自动解码来自服务器的内容

不知道链接或页面内容就无法帮助您。。。。也许你的html
find
只是wrong@OferSadan链接是错误的,为什么我的发现是错误的?我说,也许是因为你没有给我们提供数据来说明其他情况。。。例如,将链接添加到该程序对我仍然不起作用,您的
find
返回
none
请创建一个