爬虫爬取太平洋汽车的评论

本文介绍如何利用Python进行网络爬虫,详细解析爬取太平洋汽车网上汽车评论的过程,包括设置请求头、解析HTML、提取评论内容和用户信息等步骤,旨在分享网络数据抓取的技术和实战经验。
摘要由CSDN通过智能技术生成
#encoding:utf-8
import urllib
import urllib.request
from bs4 import BeautifulSoup
import re
import random
import time
import html5lib

# 设置目标url,使用urllib.request.Request创建请求
url0 = "http://price.pcauto.com.cn/comment/sg3225/"
req0 = urllib.request.Request(url0)

# 使用add_header设置请求头,将代码伪装成浏览器
req0.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0")

# 使用urllib.request.urlopen打开页面,使用read方法保存html代码
#html0 = urllib.request.urlopen(req0).read()
html0 = urllib.request.urlopen(req0)

# 使用BeautifulSoup创建html代码的BeautifulSoup实例,存为soup0
#soup0 = BeautifulSoup(html0)
soup0 = BeautifulSoup(html0.read(),"html5lib")
# 获取尾页(对照前一小节获取尾页的内容看你就明白了)
total_page = int(soup0.find("div",class_= "pcauto_page").findAll("a")[-2].get_text())
myfile = open("langyi.txt",
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值