1 Star 0 Fork 0

TTTonghe/request-study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pachong4.py 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
TTTonghe 提交于 2021-12-20 12:21 . sql
import requests #发送网络请求
import parsel
url = "https://www.che168.com/china/8_10/#pvareaid=100943"
headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
}
response = requests.get(url = url
, headers = headers)
response.encoding = 'gbk' #设置编码
date_html = response.text
#解析数据
selector = parsel.Selector(date_html)
lis = selector.css('.viewlist_ul li')
import pymysql
# 连接数据库
conn = pymysql.connect(host="localhost", user="root", passwd="123456", database="pythondata", charset="utf8")
print('服务器连接成功!')
# for li in lis:
# try:
# name = li.css('.card-name::text').get()
# unit = li.css('.cards-unit::text').get()
# kmNumber = (unit.split('/')[0])[:-3]
# years = unit.split('/')[1]
# city = unit.split('/')[2]
# business = unit.split('/')[3]
# price = li.css('.pirce em::text').get()
# price1 = float(price)
# yprice = (li.css('s::text').get())[:-1]
# yprice1 = float(yprice[:-1])
# zhejiu =str(price1/yprice1)
# # print(type(price)+" "+type(yprice))
# # chajia = yprice - price
# # zhejiu = chajia / yprice
# # print(name,kmNumber,years,city,business,price,yprice)
# cursor = conn.cursor()
# try:
# # 一次插入多条记录
# sql = "INSERT IGNORE INTO test VALUES('"+name+"','"+kmNumber+"','"+years+"','"+city+"','"+business+"',"+price+","+yprice+","+zhejiu+")";
# print(sql)
# cursor.execute(sql)
# conn.commit() # 提交更新的数据到数据库
# # print('插入成功')
# except Exception as error: # 插入失败后,抛出异常
# print('插入数据失败')
# print(error)
# # print(name)
# except:
# pass
# conn.close()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/th-free-away/request-study.git
git@gitee.com:th-free-away/request-study.git
th-free-away
request-study
request-study
master

搜索帮助