import requests
from bs4 import BeautifulSoup
import MySQLdb
house_url="https://dg.lianjia.com/ershoufang/105104955509.html"
def get_page(url):
r=requests.get(house_url)
s=BeautifulSoup(r.text,"html.parser")
return s
def get_links(link_url):
r=requests.get(house_url)
s=BeautifulSoup(r.text,"html.parser")
links_div=s.find_all("ul",class_="sellListConten")
links=[div.a.get("href")for div in links_div]
return links
def house_info(house_url):
house_url="https://dg.lianjia.com/ershoufang/105104955509.html"
s=get_page(house_url)
price=s.find("span",class_="total").text#总价格
unit=s.find("span",class_="unitPriceValue").text#每平米价格
main=s.find("div",class_="mainInfo").text#几室几厅
sub=s.find("div",class_="subInfo").text#几楼层
Info=s.find("div",class_="mainInfo",title="南")#朝向
info=s.find("div",class_="subInfo").text#精/平装
area=s.find("div",class_="mainInfo")#面积
time=s.find("div",class_="subInfo").text#建造时间
live=s.find("div",class_="communityName").text#小区
town=s.find("span",class_="info").text#市/镇
visit=s.find("span",class_="info").text#参观时间
jz=s.find("span",class_="label").text#建筑结构
a={"总价格:",price,
"每平米价格:",unit,
"几室几厅:",main,
"几楼层:",sub,
"朝向:",Info,
"精/平装:",info,
"面积:",area,
"建造时间:",time,
"建筑结构:",jz,
"参观时间:",visit,
"市/镇:",town
}
return a
links=get_links("https://dg.lianjia.com/ershoufang")
for link in links:
time.sleep(1)
print("获取信息:")
house=house_info(link)
insert(db,house)
house_url="https://dg.lianjia.com/ershoufang/105104955509.html"
print(house_info(house_url))
DATABASE={
"host":"127.0.0.1",
"user":"root",
"password":"lihaoen20091224",
"charset":"utf8m64"
}
def get_db(setting):
return MySQLdb.connect(**setting)
def insert(house_info):
values="{}"*11+"'{}'"
sql_values=vaules.format(house["价格"],house["单位"],house["面积"],house["户型"],house["楼层"],house["朝向"],house["建造时间"],house["小区"],house["市/镇"],house["参观时间"],house["建筑结构"])
sql="""
insert into h`house(`price`,`unit`,`area`,`main`,`sub`,`Info`,`time`,`live`,`info`,`town`,`visit`,`jz`)
values({})
""".format(sql)
cursor=db.cursor()
cursor.excute(sql)
db.commit()
house=house_info(house_url)
db=get_db(DATABASE)
错误提示:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\python\12.02.01.py", line 70, in <module>
db=get_db(DATABASE)
File "C:\Users\Administrator\Desktop\python\12.02.01.py", line 58, in get_db
return MySQLdb.connect(**setting)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\MySQLdb\__init__.py", line 84, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\MySQLdb\connections.py", line 179, in __init__
super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2019, "Can't initialize character set utf8m64 (path: C:\\Program Files\\MySQL\\MySQL Server 6.1\\share\\charsets\\)")
请问是哪里输错了?