网络抓取价格和手机名称?

时间:2020-09-02 06:33:04

标签: python html selenium web-scraping beautifulsoup

我是python的新手,我正尝试从flipkart抓取特定手机的名称和价格。

from requests import get
from bs4 import BeautifulSoup
import pandas as pd

name=[]
price=[]
rating=[]


url="https://www.flipkart.com/search?sid=tyy%2C4io&otracker=CLP_Filters&p%5B%5D=facets.ram%255B%255D%3D4%2BGB&p%5B%5D=facets.rating%255B%255D%3D4%25E2%2598%2585%2B%2526%2Babove"

results=requests.get(url)

soup=BeautifulSoup(results.text,"html.parser")

soup.prettify()

details=soup.find_all("div",attrs={"class":"bhgxx2 col-12-12"})

for mobiles in details:
    mob_name=mobiles.find("div",attrs={"class":"_3wU53n"})
    name.append(mob_name.text)
    print(name)

输出:

第30行中的文件“ C:/ Users / hp / Desktop / mobile scrapping.py” name.append(mob_name.text)

AttributeError:'NoneType'对象没有属性'text'

1 个答案:

答案 0 :(得分:2)

请检查一下

import requests
from bs4 import BeautifulSoup
url="https://www.flipkart.com/search?sid=tyy%2C4io&otracker=CLP_Filters&p%5B%5D=facets.ram%255B%255D%3D4%2BGB&p%5B%5D=facets.rating%255B%255D%3D4%25E2%2598%2585%2B%2526%2Babove"
results=requests.get(url)
soup=BeautifulSoup(results.text,"html.parser")
mobiles=[]
rates=[]
details=soup.findAll("div",attrs={"class":"_3wU53n"})
for i in details:
    mobiles.append(i.text)
prices=soup.findAll("div",attrs={"class":"_1vC4OE _2rQ-NK"})
for j in prices:
    rates.append(j.text)
final=[[x,y] for x,y in zip(mobiles,rates)]
print(final)

输出:['Realme Narzo 10A(So White,64 GB)','₹9,999'],['Realme Narzo 10(That Blue,128 GB)','₹11,999']]