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)
添加回答
举报