为啥会出现AttributeError: 'str' object has no attribute 'jion'
importbs4 #网页解析,获取数据
import re
import urllib.request,urllib.error#制定URL,获取网页数据
import xlwt #进行excel 操作
import sqlite3#进行Sqlite数据库操作
def main():
baseurl = "https://movie.douban.com/top250?start=0"
#1.爬取网页
datalist = getData(baseurl)
# savepath = ".\\豆瓣电影top250.xls"
dbpath = "movie.db"
#3.保存数据
#saveData(datalist,savepath)
saveData2Db(datalist,dbpath)
findLink = re.compile(r'<a href="(.*?)">') #创建正则表达式对象,表示规则<a href="https://movie.douban.com/subject/1292052/">
findImgScr = re.compile(r'<img.*src="(.*?)',re.S)
findTitle = re.compile(r'<span class="title">(.*?)</span>')
findRating = re.compile(r'<span class="rating_num" property="v:average">(.*)</span>')
findJudge = re.compile(r'<span>(\d*)人评价</span>')
findInq = re.compile(r'<span class="inq">(.*)</span>') #概况
findBd = re.compile(r'<p class="">(.*?)</p',re.S) #影片相关内容
#1.爬取网页
def getData(baseurl):
datalist = []
for i in range(0,10): #调用获取页面的信息函数,10次
url = baseurl + str(i*25)
html = askURL(url) #保存获取到的网页源码
#2.逐一解析数据
soup = bs4.BeautifulSoup(html,"html.parser")
for item in soup.find_all('div',class_="item"):#查找符合要求的字符串,记得class加“_"
# print(item)#测试查看电影item的所有信息
data = [] #保存一部电影的所有信息
item = str(item)
#影片详情的信息
link = re.findall(findLink,item) #查找指定的字符串,0表示第一个findLink字符串
# print(link) #测试
data.append(link)
imagSrc = re.findall(findImgScr,item)
data.append(imagSrc)
titles = re.findall(findTitle,item)
if(len(titles) == 2):
ctitle = titles #添加中文名
data.append(ctitle)
otitle = titles.replace("/","")#去掉无关的符号
data.append(otitle)
else:
data.append(titles)
data.append(' ') #外国名留空
rating = re.findall(findRating,item)
data.append(rating)
judgeNum = re.findall(findJudge,item)
data.append(judgeNum)
inq = re.findall(findInq,item)
if len(inq) != 0:
inq = inq.replace(".","") #去掉句号
data.append(inq)
else:
data.append(" ")
bd = re.findall(findBd,item)
bd = re.sub('<br(\s+)?>(\s+)?'," ",bd) #去掉<br/>正则表达式\s表示空白符,第二个表示换之后
bd = re.sub('/'," ",bd)
data.append(bd.strip()) #strip去掉前后的空格
datalist.append(data) #把处理好的一部电影信息放入datalist
return datalist
#得到指定的URL网页内容
def askURL(url):
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36 Edg/89.0.774.77"
}
requset = urllib.request.Request(url,headers = header)
html = ""
try:
response = urllib.request.urlopen(requset)
html = response.read().decode("utf-8")
#print(html)
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
return html
#保存数据
def saveData(savepath,datalist):
print()
book = xlwt.workbook(encoding="utf-8",style_compression=0)#创建workbook对象,样式压缩效果
sheet = book.add_sheet('豆瓣电影top250',cell_overwrite_ok=True)#创建工作表,每个单元格覆盖以前的内容
col = ('电影详情链接','图片链接','影片中文名','影片外国名','评分','评价数','概况','相关信息')
for i in range(0,8):
sheet.write(0,i,col) #,列名
for i in range(0,250):
print("第%d条" %i)
data = datalist
for j in range(0,8):
sheet.write(i+1,j,data)#数据
book.save('student.xls') #保存
def saveData2Db(datalist,dbpath):
init_db(dbpath)
conn = sqlite3.connect(dbpath)
cur = conn.cursor()
for data in datalist:
for index in range(len(data)):
if index == 4 or index == 5:
continue
data = '"'+data+'"'
sql = '''insert into movie250(
info_link,pic_link,cname,ename,score,rated,instrodcution,info)
values(%s)'''%",".jion(data)#每一条加逗号
print(sql)
cur.excute(sql)
conn.commit()
cur.close()
conn.close()
def init_db(dbpath):
sql ='''create table movie250
(id integer primary keyautoincrement,
info_link text,
pic_link text,
cnane vachar,
ename vachar,
score numric,
rated numeric,
instroduction text,
info text
)
'''#创建数据库
conn = sqlite3.connect(dbpath)
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
conn.close()
if __name__ == "__main__":
main()
# init_db("movietest.db") #测试
print("爬取完成")
================================================
Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\douban-\venv\spider.py", line 158, in <module>
main()
File "C:\Users\Administrator\PycharmProjects\douban-\venv\spider.py", line 21, in main
saveData2Db(datalist,dbpath)
File "C:\Users\Administrator\PycharmProjects\douban-\venv\spider.py", line 130, in saveData2Db
values(%s)'''%",".jion(data)#每一条加逗号
AttributeError: 'str' object has no attribute 'jion'
jion orjoin? join吧? 代码小白liu 发表于 2021-4-20 15:31
join吧?
是的,谢谢,自己一直找不到原因{:5_90:} wp231957 发表于 2021-4-20 15:30
jion orjoin?
眼瞎了,谢谢
页:
[1]