import requests
from bs4 import BeautifulSoup
import time
def gethtml(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}
try:
r=requests.get(url,headers)
r.raise_for_status()
r.encoding='utf-8'
html=r.text
print('成功')
return html
except:
print('获取网页失败')
#解析html
def parserhtml(html,f):
soup=BeautifulSoup(html,'lxml')
dl_list=soup.find_all('dl',class_='service-comment-con')
for i in dl_list:
comment=i.find('dd',class_='w-600').text
money=i.find('dd',class_='money').text
f.writelines([comment,'\t',money])
f.writelines('\n')
def main():
start_url='http://shop.zbj.com/service/evaluationlist-sid-227487-uid-7074595-type-2-page-{page}.html?isauto=1'
f = open('D:\\爬虫练习\\光荣网络.txt', 'w',encoding='utf-8')
for page in range(1,423):
url=start_url.format(page=page)
print('正在爬取第'+str(page)+'页')
html=gethtml(url)
parserhtml(html,f)
time.sleep(3)
f.close()
main()