http://quote.eastmoney.com/ 提供了股票信息查询服务;编个仅用于学习的小爬虫,随机爬取代码以自己学号后2位结尾的股票信息,输出相关信息,输出样式: 爬取时间:2021年07月xx日 xx:yy:zz 我的学号后两位为:xx, 相应股票信息为:(以04结尾为例)
时间: 2023-06-17 12:02:46 浏览: 40
好的,这个任务可以用 Python 的 requests 和 BeautifulSoup 库来实现。首先需要安装这两个库:
```
pip install requests
pip install beautifulsoup4
```
然后就可以开始编写爬虫了:
```python
import requests
from bs4 import BeautifulSoup
import random
import time
# 获取当前时间
now = time.localtime()
current_time = time.strftime("%Y年%m月%d日 %H:%M:%S", now)
# 学号后两位
num = "04"
# 构造 URL
url = "http://quote.eastmoney.com/stocklist.html#sz" + num
# 发送请求
response = requests.get(url)
# 解析 HTML
soup = BeautifulSoup(response.text, "html.parser")
# 获取股票信息
stocks = soup.select("div#quotesearch ul li a")
# 随机选择一只股票
stock = random.choice(stocks)
# 提取股票代码和名称
code = stock["href"].split("/")[-1].split(".")[0]
name = stock.text
# 输出结果
print("爬取时间:{}".format(current_time))
print("我的学号后两位为:{}".format(num))
print("相应股票信息为:{} {}".format(code, name))
```
这个爬虫会随机选择一只以你学号后两位结尾的股票,输出其代码和名称。你只需要将 `num` 变量改成你的学号后两位即可。注意这个爬虫只是用于学习,不要用于商业用途或侵犯他人权益。