欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python爬虫基础 --爬取股吧前十页数据

程序员文章站 2022-05-02 22:13:53
...

新建文件夹 ./guba/ 爬取的十页数据会自动存到guba文件夹下

import requests
import os
for i in range(10):
    base_url = 'http://guba.eastmoney.com/default,99_'f'{i}.html'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36',
    }
    filename = './guba/'
    if not os.path.exists(filename):
        os.mkdir(filename)
    response = requests.get(base_url, headers=headers)
    with open(filename + '/{}.html'.format(i + 1), 'w', encoding='utf-8') as fp:
        fp.write(response.text)
相关标签: 爬虫