【发布时间】:2020-08-11 17:43:47
【问题描述】:
所以这是我运行良好的简单功能。基本上它是从这个站点提取数据。
def mega():
pageNumber = 1
nextPage = True
proList = []
while nextPage:
url = "http://www.mega.pk/mobiles/{}/".format(pageNumber)
response = get(url)
soup = BeautifulSoup(response.text, features="lxml")
ul = soup.find('ul', class_='item_grid list-inline clearfix')
li = ul.find_all('li', class_='col-xs-6')
if not li:
break
for link in li:
if link.find("div", class_="was"):
title = link.find(id="lap_name_div").text.replace("\n", "")
data = link.find("div", class_="cat_price").text.replace(
"\n", "").replace("\t", "").replace(" ", "")
lists = data.split("-")
if len(lists) > 2:
price = lists[1].replace("PKR", "") + "-PKR"
else:
price = data
price = int(price.replace(",", "").replace(
'-', "").replace("PKR", ""))
productUrl = link.find("a")['href']
image = link.find("img")['data-original']
pro = Product(title=title, price=price, productUrl=productUrl,imageUrl=image, site="mega.pk")
proList.append(pro)
pageNumber = pageNumber + 1
return proList
但以下是我尝试分配 item.price = product.price 时的观点。然后不是更新它而是创建新实例。
def scraper(request):
products = mega()
proList = Product.objects.all()
for product in products:
for item in proList:
if product.title == item.title and product.price == item.price:
break
elif product.title == item.title and product.price != item.price:
notify = Notification(user = request.user, changeMessage="Price updated")
notify.save()
item.price = product.price
break
return redirect("home")
【问题讨论】:
标签: python-3.x django django-rest-framework django-views python-requests