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

新建文件夹 ./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)

你可能感兴趣的:(爬虫)