大神帮帮我把 真不会啊

萧瑟的秋季 2018-05-01 12:24:07
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 7 21:05:03 2017

@author: Administrator
"""

import urllib.request
from bs4 import BeautifulSoup
import pymysql.cursors

'''
目标:获取酒店名称和酒店星级
'''
url1 = 'http://hotels.ctrip.com/hotel/435383.html'

html1 = urllib.request.urlopen(url1).read().decode('utf-8')

#1.获取酒店名称信息
soup1 = BeautifulSoup(html1,'lxml')

result1 = soup1.find_all(attrs={"itemprop":"name"})
hotelName = result1[0].string
print("酒店名称为:{}".format(hotelName))

#2.获取酒店星级信息
soup12 = BeautifulSoup(html1,'lxml')

result12 = soup12.find_all(attrs={"class":"grade"})

#print(result1)
result12 =str(result12)

soup13 = BeautifulSoup(result12,'lxml')

result13 = soup13.find_all('span')
hotelStar = result13[0]['title']
print("酒店星级为:{}".format(hotelStar))

'''
目标:获取酒店最低房价和评论总数
'''
url2 = "http://m.ctrip.com/html5/hotel/HotelDetail/435383.html"

html2 = urllib.request.urlopen(url2).read().decode('utf-8')


'''
目标:获取酒店卫生评分、环境评分、服务评分、设施评分、用户推荐比、用户评分、评价内容
'''

url3 = 'http://m.ctrip.com/html5/hotel/HotelDetail/dianping/435383.html'

html3 = urllib.request.urlopen(url3).read().decode('utf-8')

soup3 = BeautifulSoup(html3,'lxml')

#获取酒店各项评分数据
result32 = soup3.find_all(attrs={"class":"ve-txt"})
result32 = str(result32)
soup32 = BeautifulSoup(result32,'lxml')

result33 = soup32.find_all('em')
userRecommendRate = result33[0].string
hRating = result33[1].string
eRating = result33[2].string
sRating = result33[3].string
iRating = result33[4].string

print("用户推荐为:{}".format(userRecommendRate))
print("卫生评分为:{}分".format(hRating))
print("环境评分为:{}分".format(eRating))
print("服务评分为:{}分".format(sRating))
print("设施评分为:{}分".format(iRating))

#提取用户评论数据
result34 = soup3.find_all(attrs={"class":"hotel-cell-num"})
result34 = str(result34[1])
soup33 = BeautifulSoup(result34,'lxml')
result35 = soup33.find_all('p')

for i in range(0,10):
#userName = result35[i].get_text()
result36 = soup3.find_all(attrs={"itemprop":"datePublished"})
datePublished = result36[i].string
print("评论发表于:{}".format(datePublished))

result37 = soup3.find_all(attrs={"itemprop":"ratingValue"})
userRating = result37[i].string
print("评分为:{}".format(userRating))

result38 = soup3.find_all(attrs={"class":"tree-ellips-line6 js_arr"})
commentText = result38[i].get_text()
print("评论内容为:{}".format(commentText))

'''
数据库操作
'''

#获取数据库链接
connection = pymysql.connect(host = 'localhost',
user = 'root',
password = '123456',
db = 'xiecheng',
charset = 'utf8mb4')
try:
#获取会话指针
with connection.cursor() as cursor:
#创建sql语句
sql = "insert into `macro-polo` (`hotelName`,`hotelStar`,`lowPrice`,`commentCounts`,`userRecommendRate`,`hRating`,`eRating`,`sRating`,`iRating`,`datePublished`,`userRating`,`commentText`) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"

#执行sql语句
cursor.execute(sql,(hotelName,hotelStar,lowPrice,commentCounts,userRecommendRate,hRating,eRating,sRating,iRating,datePublished,userRating,commentText))

#提交数据库
connection.commit()
finally:
connection.close()

File "C:/Users/fall/PycharmProjects/untitled1/venv/hello world.py", line 85, in <module>
datePublished = result36[i].string
IndexError: list index out of range
怎么改都不对。。。。刚学爬虫
...全文
1575 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Q446512799 2018-05-28
  • 打赏
  • 举报
回复
只是报的越界啊,数组元素取多了
oyljerry 2018-05-02
  • 打赏
  • 举报
回复
result36 估计并没有拿到那么多元素,所以越界了
乞丐的生鱼片 2018-05-02
  • 打赏
  • 举报
回复
你把原始信息打出来,越界了。
萧瑟的秋季 2018-05-01
  • 打赏
  • 举报
回复
python3.6.4
小白利用PHP+MYSQL+HTML做的期末小项目,做的不好大家勿喷,还请多多指正。 用的是以前模仿做的HTML淘宝网静态页面,接连上数据库加入图片和信息,实现后台与前端的连接,实现了增删改查功能美化做的不好还请大家勿喷。呜呜呜,做了很久也只能实现这样一个效果了,要是有大神希望帮帮我改改项目,静态页面的美化会做,但是连接上PHP数据库后出来的一个效果如何美化就不知道如何才能实现了,希望有大佬教教我,可以看看我的整体一个作品,至少基础功能都可以实现,里面还放了几张同学的照片哈哈哈,皮一下,看到了不要骂我宝贝……最后一个重中之重,如何美化界面还请大神指点啊哈哈哈哈…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧