爬虫入门学习:爬取股票论坛信息

准备工作

安装requests、bs4库。
获取headers,目标网址guba.eastmoney.com,
先打开一个网页标签,进入开发人员工具,选择network网络,
在这里插入图片描述
然后进入目标网址,找到下图所示的
在这里插入图片描述
点击可得,在这里插入图片描述

正式爬取

此处选择茅台的讨论区进行爬取,其url为http://guba.eastmoney.com/list,600519_1.html。
首先测试能否访问网址,

import requests
import csv
from requests.exceptions import RequestException
from bs4 import BeautifulSoup

headers = {
   
    'Host': 'guba.eastmoney.com',
    'User-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.81"
}
url = 'http://guba.eastmoney.com/list,600519_1.html'
response = requests.get(url, headers=headers)
print(response.status_code)

输出200即为成功访问。
观察网页信息及结构,获取想要的信息,

page = BeautifulSoup(response.content, "lxml")
print(page)

此处展示其中一段普通帖子的,

<div class="articleh normal_post">
<span class="l1 a1">12064</span>
<span class="l2 a2">66</span>
<span class="l3 a3"><em class="icon icon_list_hot">
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值