django网页抓取器漂亮的汤和urllib

问题描述 投票:0回答:1

[使用刮板为我的数据库获取一些数据,即使用此代码从其他站点添加产品

def scrape():
    path=''
    counter=0

    session= requests.Session()
    session.headers={
        "User-Agent":"my user agent"
    }
    url='some url'
    content=session.get(url,verify=False).content
    soup=bs4.BeautifulSoup(content,'html.parser')
    result=soup.find_all('div',{'class':'column column-block block-list-large single-item'})
    for i in result:
        counter+=1
        name=i.find_all('h1',{'class':'itemTitle'})[0]
        price=i.find('h3',{'class':'itemPrice'})
        image=i.find('img',{'class':'img-size-medium imageUrl'})['data-src']
        path=f'pics/{counter}.jpg'
        img=path
        barcode=f'name{counter}'
        description='this is my product'
        urllib.request.urlretrieve(image,path)
        cat=category.objects.get(id=140)
        br=branch.objects.get(id=8)
        products.objects.create(name=name.text,Barcode=barcode,branch=br,image=img,
        description=description,price=price,category=cat)

scrape()

正在下载产品的图像,但此后却出现错误

值= value.resolve_expression(self.query,allow_joins = False,for_save = True)

TypeError:'NoneType'对象不可调用

django python-3.x beautifulsoup urllib3
1个回答
0
投票

这很可能在创建操作中为price=price。对于该字段,price不是有效值,它是一个对象。您可以使用price=price.text更改该部分吗?

© www.soinside.com 2019 - 2024. All rights reserved.