Python Get Printed Output into Dataframe

不想你离开。 提交于 2021-02-11 14:30:38

问题


I am having trouble getting the printed output of a python script into a data frame. When I execute the script the information I want in the data frame prints, but the script creates an empty table in SQL. I believe there's something wrong with the structure of my script at the point where I try to get the results into a data frame.

---

import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse, parse_qs


headers = ({'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
            }
           )
           

desc = []

for page in range(1, 2):
    #print('Page {}...'.format(page))
    #print()
   
    url = 'https://www.landwatch.com/Michigan_land_for_sale/Land/Page-{}'.format(page)
    soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')
    
    for a in soup.select('.propName a'):
        prop_url = 'https://www.landwatch.com' + a['href']
        prop_soup = BeautifulSoup(requests.get(prop_url, headers=headers).content, 'html.parser')

        google_maps_url = prop_soup.select_one('#iframe-map iframe')
      

        title = (prop_soup.h1.text)
        this =  (parse_qs(urlparse(google_maps_url['src']).query).get('q') )
 
        desc.append(a.title + this (strip=True))   
       
import pandas as pd
df = pd.DataFrame({'description': desc}) 
import pyodbc
sql_conn = pyodbc.connect('DRIVER={SQL Server};SERVER=LAPTOP-5LE89UIK\MSSQLSERVER01;DATABASE=Dave;')
cursor = sql_conn.cursor()
cursor.execute('Create TABLE Dave..Property22 (descrip varchar(max), Acreage float, Price money, City varchar(max), County varchar(max), priceperacre float)')
sql_conn.commit()

for row in df.itertuples():
    cursor.execute('''
             INSERT INTO Dave..Property22 (descrip)
             VALUES (?); 
            ''', 
            row.description
            )
sql_conn.commit

来源:https://stackoverflow.com/questions/64122543/python-get-printed-output-into-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!