亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用Python中的鏈接抓取手機模型

用Python中的鏈接抓取手機模型

呼啦一陣風 2021-12-09 10:56:43
我正在嘗試從該網站上刪除手機型號列表https://www.m1.com.sg/personal/mobile/phones/filters/all-plans/all/all/0/1500/0/0/沒有任何這將列出型號和價格。我有以下代碼,但所有價格都不正確。他們不應該是零。我做錯了什么?此外,是否可以僅使用美麗的湯提供可點擊的鏈接(允許用戶點擊“更多信息”,將他們帶到包含手機型號附加信息的頁面)?例如: iPhone XR 128GB   $ 0    More Infoimport urllib.requestfrom bs4 import BeautifulSoupfrom html.parser import HTMLParserurl_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')Model_findall=soup.findAll("div",{"class":"td three title text-center"})price_findall=soup.findAll("div",{"class":"td two price text-center"})for models in Model_findall:    print('*',models.text.strip())    print(' ',price.text.strip())
查看完整描述

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)


查看完整回答
反對 回復 2021-12-09
?
牧羊人nacy

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)


查看完整回答
反對 回復 2021-12-09
?
開心每一天1111

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)


查看完整回答
反對 回復 2021-12-09
  • 3 回答
  • 0 關注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號