Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: Mario Lopes
Added: Jan 14, 2019 10:54 AM
Modified: Jan 14, 2019 11:03 AM
Views: 31
  1. import requests
  2. import pymysql
  3. import datetime
  4. from bs4 import BeautifulSoup
  5.  
  6. def pesquisa (url,codigo,empresa):
  7.     #print(codigo)
  8.     headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
  9.     pagina = requests.get(url, headers=headers)
  10.     soup = BeautifulSoup(pagina.content, "html.parser")
  11.     procura ="td."+codigo
  12.     preco = soup.select_one(procura)
  13.     preco_com_ponto=preco.text.replace(",",".")
  14.     #print(preco_com_ponto)
  15.     now = datetime.datetime.now()
  16.     cur.execute('''INSERT into diaria (empresa, valor,data,hora)
  17.                              values (%s, %s, %s,%s)''',
  18.                 (empresa,preco_com_ponto,now.strftime("%Y-%m-%d"),now.strftime("%H:%M:%S")))
  19.     conn.commit();