抓包爬猪八戒网评论

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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值