2017-12-13 4 views
1

매우 정기적 인 구매를해야하고 직접 구입하기가 번거롭기 때문에 항목을 장바구니에 추가하고 나 구입할 봇을 작성하려고합니다.체크 아웃 제품에 아름다운 스프 저장 세션

from bs4 import BeautifulSoup 
import requests 
import numpy as np 


page = requests.get("http://www.onlinestore.com/shop") 
soup = BeautifulSoup(page.content, 'html.parser') 
try: 
    for i in soup.find_all('a'): 

     if "shop" in i['href']: 
      shop_page = requests.get("http://www.onlinestore.com" + i['href']) 
      item_page = BeautifulSoup(shop_page.content, 'html.parser') 
      for h in item_page.find_all('form', class_="add"): 
       print(h['action']) 
       try: 
        shop_page = requests.get("http://www.online.com" + h['action']) 
       except: 
        print("None left") 
      for h in item_page.find_all('h1', class_="protect"): 
       print(h.getText()) 
except: 
    print("either ended or error occured") 
checkout_page = requests.get("http://www.onlinestore.com/checkout") 

checkout = BeautifulSoup(checkout_page.content, 'html.parser') 
for j in checkout.find_all('strong', id_="total"): 
    print(j) 

항목이 넘겨지지 않기 때문에 제품을 확인하는 데 약간의 문제가있었습니다. 장바구니에 추가 한 항목을 추적 할 수 있도록 쿠키를 구현할 수있는 방법이 있습니까?

감사

답변

0

에도 requests하지만 브라우저하지 않습니다, 그것은 여전히 ​​여러 개의 요청 전반 헤더와 쿠키를 지속 할 수 있지만, 당신은 Session 사용하는 경우 :

with requests.Session() as session: 
    page = session.get("http://www.onlinestore.com/shop") 

    # use "session" instead of "requests" later on 

주 당신은 또한 성능을 얻을 것이라고 무료 부스트 영구 연결의 이유는

당신이 제작하는 경우의 동일한 호스트에 everal 요청, 기본 TCP 연결이 특정 온라인 상점에 카트 체크 아웃 구현이 수도 있고 방법에 따라 크게 성능 향상

을 초래할 수있는, 다시 사용됩니다 작동하지 않을 수 있습니다. 그러나 적어도 이것은 앞으로 나아갈 단계입니다.