下面以四種方法去解析數據,前面三種以插件庫來解析,第四種以正則表達式去解析。html
xpathpython
pyquery正則表達式
respa
爬取信息:名稱 評分 小評插件
#python 使用xpath解析數據 #查詢豆瓣top250電影 #獲取信息:名稱 評分 短語 #關於xpath語法:https://www.w3school.com.cn/xpath/xpath_syntax.asp from lxml import etree import time import requests import os #建立文件 t = time.strftime('%Y-%m-%d', time.localtime()) # 將指定格式的當前時間以字符串輸出 suffix = ".txt" newfile ="./log/xpath_"+ t + suffix if not os.path.exists(newfile): f = open(newfile, 'w',encoding="utf-8") f.close() #打開文件,準備寫入信息 f = open(newfile, 'w',encoding="utf-8") start=0 while start<250: # 查詢top250電影,第頁25條,取10頁 r=requests.get("https://movie.douban.com/top250?start="+str(start) +"&filter=") el=etree.HTML(r.content) r.close() #解析內容 el_items=el.xpath('//div[@class="item"]') for item in el_items: #當獲取子項信息時,xpath開頭不能以「/」或「//」開頭,「//」是查詢整個html。開始必定要指當前子項,後面能夠使用「/」或「//」來搜索 title=item.xpath('div//span[@class="title"][1]/text()')[0] #標題 rating_num=item.xpath('div//span[@class="rating_num"][1]/text()')[0]#評分 # 小評可能不存在,在此加判斷 inq=item.xpath('div//span[@class="inq"][1]/text()')#小評 inq_str="" if len(inq)>0: inq_str=inq[0] #寫入文件 f.write(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n") start+=25 #最後關閉文件 f.close() print("the end")
#python 使用pyquery解析數據 #查詢豆瓣top250電影 #獲取信息:名稱 評分 短語 #關於pyquery語法:https://pyquery.readthedocs.io/en/latest/pseudo_classes.html from pyquery import PyQuery as pq import time import requests import os #建立文件 t = time.strftime('%Y-%m-%d', time.localtime()) # 將指定格式的當前時間以字符串輸出 suffix = ".txt" newfile ="./log/pyquery_"+ t + suffix if not os.path.exists(newfile): f = open(newfile, 'w',encoding="utf-8") f.close() #打開文件,準備寫入信息 f = open(newfile, 'w',encoding="utf-8") start=0 while start<250: #查詢top250電影,第頁25條,取10頁 r = requests.get("https://movie.douban.com/top250?start=" + str(start) + "&filter=") d=pq(r.content) r.close() items=d('.item') for item in items: item_d=pq(item)#從新加載每一項html,爲下面取出信息 title= item_d.find(".title:eq(0)").text()#名稱 rating_num =item_d.find(".rating_num:eq(0)").text()# 評分 inq_str = item_d.find('.inq:eq(0)').text() # 小評 # 寫入文件 f.write(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n") start+=25 #最後關閉文件 f.close() print("the end")
#python 使用BeaufifulSoup解析數據 #查詢豆瓣top250電影 #獲取信息:名稱 評分 短語 #關於語法:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ from bs4 import BeautifulSoup import time import requests import os #建立文件 t = time.strftime('%Y-%m-%d', time.localtime()) # 將指定格式的當前時間以字符串輸出 suffix = ".txt" newfile ="./log/BeaufifulSoup_"+ t + suffix if not os.path.exists(newfile): f = open(newfile, 'w',encoding="utf-8") f.close() #打開文件,準備寫入信息 f = open(newfile, 'w',encoding="utf-8") start=0 while start<250: #查詢top250電影,第頁25條,取10頁 r=requests.get("https://movie.douban.com/top250?start="+str(start) +"&filter=") el=BeautifulSoup(r.content,"xml") r.close() items=el.find_all("div", class_="item")#獲取一項電影信息 for item in items: title=item.find_all(class_="title",limit=1)[0].get_text()#名稱 rating_num=item.find_all('span',class_="rating_num",limit=1)[0].get_text() # 評分 # 小評可能不存在,在此加判斷 inq = item.find_all('span',class_="inq",limit=1) # 小評 inq_str = "" if len(inq) > 0: inq_str = inq[0].get_text() f.write(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n") #print(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n") start+=25 #最後關閉文件 f.close() print("the end")
#python 使用re正則匹配 #查詢豆瓣top250電影 #獲取信息:名稱 評分 短語 import re import time import requests import os reg_items=re.compile('<li>[\r\n\s]+<div\s+class="item">[.\r\n\s\S]*?</li>')#每一個電影 reg_title=re.compile('(?<=title">)[^<]+')#電影名稱 reg_rating_num=re.compile('(?<=property="v:average">)[^<]+')#評分 reg_inq=re.compile('(?<=class="inq">)[^<]+')#小評 #建立文件 t = time.strftime('%Y-%m-%d', time.localtime()) # 將指定格式的當前時間以字符串輸出 suffix = ".txt" newfile ="./log/re_"+ t + suffix if not os.path.exists(newfile): f = open(newfile, 'w',encoding="utf-8") f.close() #打開文件,準備寫入信息 f = open(newfile, 'w',encoding="utf-8") start=0 while start<250: #查詢top250電影,第頁25條,取10頁 r = requests.get("https://movie.douban.com/top250?start=" + str(start) + "&filter=") html=str(r.content,encoding = "utf-8") r.close() maths= reg_items.findall(html) for item in maths: re_title=reg_title.search(item) title=re_title.group(0) re_rating_num=reg_rating_num.search(item) rating_num=re_rating_num.group(0) inq_str="" #小評可能不存在,在此加判斷 re_inq=reg_inq.search(item) if re_inq!=None: inq_str=re_inq.group(0) f.write(str(title).strip().ljust(20, '—') + str(rating_num).strip().ljust(20, ' ') + ">" + str( inq_str).strip().ljust(50, ' ') + "\n") #print(str(title).strip().ljust(20,'—')+str(rating_num).strip().ljust(20,' ')+">"+str(inq_str).strip().ljust(50,' ')+"\n") start+=25 #最後關閉文件 f.close() print("the end")
爲毛要這麼方法去解析?從衆多方式作一個比較,那種方式有優點,解析起來更方便。之後須要解析的時候,從中選擇最優的。3d
來源:http://www.javashuo.com/article/p-rvztuysa-by.html 黑白記憶code