3 回答

TA貢獻1799條經驗 獲得超8個贊
以下腳本應為您提供所需的輸出。
import requests
from bs4 import BeautifulSoup
url = "https://www.m1.com.sg/personal/mobile/phones/filters/all-plans/all/all/0/1500/0/0/none"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
for items in soup.find_all(class_="phone-line"):
model = items.find(class_="title").text.strip()
price = items.find(class_="light-blue").text.strip()
print(model,price)

TA貢獻1862條經驗 獲得超7個贊
你的意思是這樣嗎?
url_toscrape = "https://www.m1.com.sg/personal/mobile/phones/filters/all-plans/all/all/0/1500/0/0/none"
response = urllib.request.urlopen(url_toscrape)
info_type = response.info()
responseData = response.read()
soup = BeautifulSoup(responseData, 'lxml')
for tr in soup.find_all("div",{"class":"tr middle"}):
for model in tr.find_all("div",{"class":"td three title text-center"}):
model = model.text.strip()
for price in tr.find_all("div",{"class":"td two price text-center"}):
price = price.text.strip()
for info in tr.find_all("div",{"class":"td two description"}):
for link in info.find_all("a"):
info = info.text.strip() + ": https://www.m1.com.sg" + link['href'].replace(" ","%20")
print (model,price,info)

TA貢獻1836條經驗 獲得超13個贊
您可以使用以下 css 類和 id 選擇器
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = "https://www.m1.com.sg/personal/mobile/phones/filters/all-plans/all/all/0/1500/0/0/none"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
models = [item.text for item in soup.select('#PhoneListDiv .color-orange')]
prices = [item.text for item in soup.select('.price .light-blue')]
df = pd.DataFrame(list(zip(models, prices)), columns = ['Model', 'Price'])
print(df)
添加回答
舉報