问题
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