Javascript 无法在BeautifulSoup中获取一些详细信息
我正在使用BeautifulSoup对数据进行SCRP,除了一件事,所有的东西都在我的代码中工作,那就是价格。我正在尝试刮取一个房地产网站,但无法刮取价格。网站为“” 以下是我的代码:Javascript 无法在BeautifulSoup中获取一些详细信息,javascript,python,web-scraping,beautifulsoup,Javascript,Python,Web Scraping,Beautifulsoup,我正在使用BeautifulSoup对数据进行SCRP,除了一件事,所有的东西都在我的代码中工作,那就是价格。我正在尝试刮取一个房地产网站,但无法刮取价格。网站为“” 以下是我的代码: from urllib.request import urlopen from bs4 import BeautifulSoup import requests import time import json import io url = "https://www.proptiger.com/all-proje
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
import time
import json
import io
url = "https://www.proptiger.com/all-projects"
# for all pages https://www.proptiger.com/all-projects?page=2
html = urlopen(url)
soup = BeautifulSoup(html, "html.parser")
container = soup.find_all("section", {"class":"project-card-main-wrapper"})
print(len(container))
newFile = "Prop_Data.csv"
f = open(newFile, "w", encoding = "utf-8")
Headers = "Project, Url, City, Builder, Price\n"
f.write(Headers)
#f.close()
for i in container:
contain = i.find_all("div", {"class":"proj-name"})
project_name = contain[0]['title']
url2 = i.div['data-url']
url1 = "https://www.proptiger.com"
url = url1+url2
get_city = i.find_all("span", {"itemprop":"address"})#or by div, {"class":"loc"}
city = get_city[0]["title"]# or by getcity.text
builder = i.find_all("div", {"class":"projectBuilder put-ellipsis"})
bName = builder[0].text
price = i.find_all("div", {"class":"project-price"})
pricereal = price[0].text#not able to print the print says list out of index
print(pricereal)
#f.write("{}".format(project_name) +",{}".format(url)+",{}".format(city)+",{}".format(bName)+"\n")
#f.close()
现在,每当我运行这段代码时,它都会显示列表超出范围
以下是价格的html:
₹ 32.4 L-₹ 88.0升
因为其中一个项目(Godrej Emerald)有要求的价格,因此没有价格价值。因为其中一个项目(Godrej Emerald)有要求的价格,因此没有价格价值。当您在抓取不可用的信息时,您将收到该错误。如果您正在查找价格,但没有显示特定项目的值,则会显示错误并中断代码。它被破坏是因为你的代码说那里有东西,而实际上没有
解决这个问题的方法是使用try-except语句
try:
pricereal = price[0].tex
except:
pricereal = "n/a"
当您抓取不可用的信息时,将出现该错误。如果您正在查找价格,但没有显示特定项目的值,则会显示错误并中断代码。它被破坏是因为你的代码说那里有东西,而实际上没有 解决这个问题的方法是使用try-except语句
try:
pricereal = price[0].tex
except:
pricereal = "n/a"
您没有得到价格,因为它在javascript中。看到所有其他项目都在打印,但为什么价格没有打印出来,不要感到困惑。因此,为了解决这一问题,您可以将硒与BeautifulSoup结合使用 我在这里使用了代码的必要部分:
from bs4 import BeautifulSoup
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.proptiger.com/all-projects")
time.sleep(5)
soup = BeautifulSoup(driver.page_source, "html.parser")
driver.quit()
for item in soup.find_all("section", {"class":"project-card-main-wrapper"}):
price = item.select(".project-price")[0].text if item.select(".project-price") else ""
print(price)
部分结果:
₹ 32.4 L- ₹ 88.0 L
₹ 33.6 L- ₹ 51.0 L
₹ 62.0 L- ₹ 1.25 Cr
₹ 49.9 L- ₹ 1.32 Cr
₹ 35.0 L- ₹ 50.0 L
为了让事情更清楚,请参见以下内容:
>>> import requests
>>> link = "https://www.proptiger.com/all-projects"
>>> page = requests.get(link).text
>>> 'Umang Premiere' in page
True
>>> '₹ 35.0 L' in page
False
>>>
我是在python IDE中完成的。如您所见,产品名称已找到,但价格未找到。这是因为javascript。希望它有意义。您没有得到价格,因为它在javascript中。看到所有其他项目都在打印,但为什么价格没有打印出来,不要感到困惑。因此,为了解决这一问题,您可以将硒与BeautifulSoup结合使用 我在这里使用了代码的必要部分:
from bs4 import BeautifulSoup
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.proptiger.com/all-projects")
time.sleep(5)
soup = BeautifulSoup(driver.page_source, "html.parser")
driver.quit()
for item in soup.find_all("section", {"class":"project-card-main-wrapper"}):
price = item.select(".project-price")[0].text if item.select(".project-price") else ""
print(price)
部分结果:
₹ 32.4 L- ₹ 88.0 L
₹ 33.6 L- ₹ 51.0 L
₹ 62.0 L- ₹ 1.25 Cr
₹ 49.9 L- ₹ 1.32 Cr
₹ 35.0 L- ₹ 50.0 L
为了让事情更清楚,请参见以下内容:
>>> import requests
>>> link = "https://www.proptiger.com/all-projects"
>>> page = requests.get(link).text
>>> 'Umang Premiere' in page
True
>>> '₹ 35.0 L' in page
False
>>>
我是在python IDE中完成的。如您所见,产品名称已找到,但价格未找到。这是因为javascript。希望它有意义。我用try and Exception试过,但不起作用。。。我还在想为什么!看起来Shahin做对了罪魁祸首是JavaScript。我个人在每个项目中都使用Selenium,在这样的时候使用它是很好的,也可以记录你的抓取内容和位置。我用try and Exception尝试了它,但它不起作用。。。我还在想为什么!看起来Shahin做对了罪魁祸首是JavaScript。我个人在每个项目中都使用Selenium,在这种情况下使用Selenium是很好的,而且还可以跟踪您的抓取内容和位置。嗨,可以在我的代码中这样做吗,比如如果我只想使用beautifulSoup怎么办?据我所知:不,不可能。BeautifulSoup无法处理动态生成的项。谢谢。是的,我会用硒试一试。非常感谢你,我可以用我的代码来做吗,比如如果我只想用beautifulSoup怎么办?据我所知:不,不可能。BeautifulSoup无法处理动态生成的项。谢谢。是的,那我就用硒试试。非常感谢