我正在尝试提取模型名称和有关模型的任何其他详细信息。当我尝试获取文本,然后我找不到任何特别的,我可以用它来获取数据。
有人知道如何从这些url中获取数据吗?
https://www.audi.de/de/brand/de.html
或https://www.opel.de/auswahlhilfe/modelle.html
我想获得的型号列表,并且可以像价格的任何财产。
到目前为止,我正在尝试使用此功能:
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
from urllib.request import urlopen
url = "https://www.opel.de/auswahlhilfe/modelle.html"
html = urlopen(url)
text = soup.get_text()
但是我没有得到任何有用的信息。这里有专家吗?
答案 0 :(得分:3)
如果转到“网络”选项卡,您将获得以下链接,该链接以json格式返回值。 您不需要硒。
https://www.opel.de/apps/atomic/getVehicleTeasers.path=L2NvbnRlbnQvb3BlbC93b3JsZHdpZGUvZ2VybWFueS9kZS9pbmRleC9iYXNlYmFsbC1jYXJkcy9iYmMtY29sbGVjdGlvbnMvdmVoaWNsZXMtb25seS1jb2xsZWN0aW9uPuGlIfE.feefoEnabled=false.expandingMenuEnabled=false.json
尝试下面的代码。
from bs4 import BeautifulSoup
import requests
import pandas as pd
url='https://www.opel.de/apps/atomic/getVehicleTeasers.path=L2NvbnRlbnQvb3BlbC93b3JsZHdpZGUvZ2VybWFueS9kZS9pbmRleC9iYXNlYmFsbC1jYXJkcy9iYmMtY29sbGVjdGlvbnMvdmVoaWNsZXMtb25seS1jb2xsZWN0aW9uPuGlIfE.feefoEnabled=false.expandingMenuEnabled=false.json'
rs=requests.get(url).json()
html=''.join(rs['bbcTeaser'])
soup=BeautifulSoup(html,'html.parser')
car_name=[]
car_price=[]
for name, price in zip(soup.select('.q-carline'),soup.select('.q-value')):
car_name.append(name.text)
car_price.append(price.text)
df = pd.DataFrame({"car_name":car_name,"car_price":car_price})
print(df)
输出:
car_name car_price
0 ADAM € 14.120,00 nur Lagerfahrzeuge
1 ADAM ROCKS € 16.475,00 nur Lagerfahrzeuge
2 ADAM ROCKS S € 20.430,00
3 ADAM S € 19.330,00
4 Ampera-e € 42.990,00
5 Astra 5-Türer € 19.990,00
6 Astra Sports Tourer € 20.990,00
7 Cascada € 33.995,00 nur Lagerfahrzeuge
8 Combo Life € 21.645,00
9 Neuer Corsa € 13.990,00
10 Corsa-e € 29.900,00
11 Corsa 3-Türer € 13.255,00
12 Corsa 5-Türer € 14.055,00
13 Crossland X € 18.750,00
14 Grandland X € 24.700,00
15 Insignia Grand Sport € 28.505,00
16 Insignia GSi € 46.695,00
17 Insignia Sports Tourer € 29.505,00
18 Insignia Country Tourer € 41.385,00
19 KARL € 13.350,00
20 KARL ROCKS € 12.965,00
21 Mokka X € 20.495,00
22 Zafira € 28.495,00
23 Zafira Life € 34.780,00
24 Combo Cargo € 20.230,00
25 Movano Cargo € 27.925,00
26 Movano Doppelkabine € 38.288,25
27 Movano Fahrgestell Normalkabine € 34.777,75
28 Movano Fahrgestell Doppelkabine € 35.967,75
29 Movano Plattformfahrgestell € 34.777,75
30 Movano Kofferaufbau € 46.320,75
31 Movano Pritsche Normalkabine € 37.574,25
32 Movano Pritsche Doppelkabine € 38.764,25
33 Movano Kofferaufbau € 46.320,75
34 Movano Pritsche Normalkabine € 37.574,25
35 Movano Pritsche Doppelkabine € 38.764,25
36 Movano Kipper Normalkabine € 39.894,75
37 Movano Kipper Doppelkabine € 44.178,75
38 Vivaro Cargo € 29.750,00
39 Vivaro Doppelkabine € 33.082,00
40 Vivaro Kombi € 31.237,50
41 Grandland X Hybrid4 € 51.165,00
42 Movano Kombi € 30.905,00
快照:
答案 1 :(得分:1)
我不确定您是否致力于使用BeautifulSoup,但是您可以使用一些普通的旧硒来解决此问题。
使用您提供的网址https://www.opel.de/auswahlhilfe/modelle.html,这些示例可能会帮助您入门。
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# start the driver
driver = webdriver.Chrome()
# navigate to the URL
driver.get("https://www.opel.de/auswahlhilfe/modelle.html")
# invoke WebDriverWait to wait for page to load
# get the model names
model_names = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.XPATH, "//a/div/div/div/span[contains(@class, 'q-carline')]")))
for model in model_names:
print(model.text)
要获取价格列表:
prices = driver.find_elements_by_xpath("//a/div/div/div/span[contains(@class, 'q-price')]/span[contains(@class, 'q-value')]");
for price in prices:
print(price.text)
答案 2 :(得分:1)
使用hits
而不是requests
...
urllib.request
答案 3 :(得分:1)
您可以从json响应中获取一些数据。只是需要钓鱼从那里可以得到什么:
import requests
from bs4 import BeautifulSoup
url = 'https://www.opel.de/apps/atomic/getVehicleTeasers.path=L2NvbnRlbnQvb3BlbC93b3JsZHdpZGUvZ2VybWFueS9kZS9pbmRleC9iYXNlYmFsbC1jYXJkcy9iYmMtY29sbGVjdGlvbnMvdmVoaWNsZXMtb25seS1jb2xsZWN0aW9uPuGlIfE.feefoEnabled=false.expandingMenuEnabled=false.json'
jsonData = requests.get(url).json()['bbcTeaser']
for each in jsonData:
soup = BeautifulSoup(each)
carline = soup.find('span', {'class':'q-carline'}).text
price = soup.find('span', {'class':'q-value'}).text
print (carline, price)
输出:
ADAM € 14.120,00 nur Lagerfahrzeuge
ADAM ROCKS € 16.475,00 nur Lagerfahrzeuge
ADAM ROCKS S € 20.430,00
ADAM S € 19.330,00
Ampera-e € 42.990,00
Astra 5-Türer € 19.990,00
Astra Sports Tourer € 20.990,00
Cascada € 33.995,00 nur Lagerfahrzeuge
Combo Life € 21.645,00
Neuer Corsa € 13.990,00
Corsa-e € 29.900,00
Corsa 3-Türer € 13.255,00
Corsa 5-Türer € 14.055,00
Crossland X € 18.750,00
Grandland X € 24.700,00
Insignia Grand Sport € 28.505,00
Insignia GSi € 46.695,00
Insignia Sports Tourer € 29.505,00
Insignia Country Tourer € 41.385,00
KARL € 13.350,00
KARL ROCKS € 12.965,00
Mokka X € 20.495,00
Zafira € 28.495,00
Zafira Life € 34.780,00
Combo Cargo € 20.230,00
Movano Cargo € 27.925,00
Movano Doppelkabine € 38.288,25
Movano Fahrgestell Normalkabine € 34.777,75
Movano Fahrgestell Doppelkabine € 35.967,75
Movano Plattformfahrgestell € 34.777,75
Movano Kofferaufbau € 46.320,75
Movano Pritsche Normalkabine € 37.574,25
Movano Pritsche Doppelkabine € 38.764,25
Movano Kofferaufbau € 46.320,75
Movano Pritsche Normalkabine € 37.574,25
Movano Pritsche Doppelkabine € 38.764,25
Movano Kipper Normalkabine € 39.894,75
Movano Kipper Doppelkabine € 44.178,75
Vivaro Cargo € 29.750,00
Vivaro Doppelkabine € 33.082,00
Vivaro Kombi € 31.237,50
Grandland X Hybrid4 € 51.165,00
Movano Kombi € 30.905,00