基于bs4库和re库的天天基金网python爬虫

本文介绍了作者花费一晚时间调试并成功实现的天天基金网Python爬虫程序,主要涉及BeautifulSoup库和正则表达式(re库)的运用,旨在提升对这两者的技术掌握,作为学习记录。
摘要由CSDN通过智能技术生成
  • 花了一晚上的时间调试,练习,终于把这个程序改动好了
  • 通过这个更熟悉了BeautifulSoup库的用法,也练习了正则表达式的使用

import requests
from bs4 import BeautifulSoup
import bs4
import traceback
import re


def getHTMLText(url, code="utf-8"):
    try:
        Headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
        }
        r = requests.get(url, headers=Headers, timeout=30)
        r.raise_for_status()
        r.encoding = code
        return r.text
    except:
        return "网页访问失败"


def getFundList(lst, fundURL):
    html = getHTMLText(fundURL, "GB2312")
    soup = BeautifulSoup(html, 'html.parser')
    tr = soup.find_all('tr')
    for i in tr:
        try:
            id = i.attrs['id']
            lst.append(re.findall(r"\d{6}", id)[0])
        except:
            continue


def getFundInfo(lst, fundURL, fpath):
    count = 0
    for fund_code in lst:
        url = fundURL + fund_code + ".html"
        html = getHTMLText(url)
        try:
            if html == "":
                continue
            infoDict = {}
            soup = BeautifulSoup(html, 'html.parser')
            FundInfo = soup.find('div', attrs={'class': 'merchandiseDetail'})
            if isinstance(FundInfo, bs4.element.Tag):
                name = FundInfo.find_all(attrs={'class': "fundDetail-tit"})[0]
                infoDict.update({'基金名称': name.text})

            keyList = FundInfo.find_all('dt')
            valueList = FundInfo.find_all('dd')
            for i in range(len(keyList)):
                if i==0:
                    key = re.findall(r"^净值估算.{16}", keyList[i].text)[0]
                    val = re.findall(r"[\+\-]\d.\d\d+%$", valueList[3 * i].text)[0], valueList[3 * i + 1].text, valueList[3 * i + 2].text
                    infoDict[key] = val
                else:
                    key = keyList[i].text
                    val = valueList[3*i].text,valueList[3*i+1].text,valueList[3*i+2].text
                    infoDict[key] = val

            with open(fpath, 'a', encoding='utf-8') as f:
                f.write(str(infoDict) + '\n')
                count = count + 1
                if count > 100:
                    break
                print("\r当前进度: {:.2f}%".format(count * 100 / 100), end="")
        except:
            count = count + 1
            print("\r当前进度: {:.2f}%".format(count * 100 / 100), end="")
            # traceback.print_exc()  # 获得错误信息
            continue


def main():
    Fund_list_url = 'https://fund.eastmoney.com/fund.html#os_0;isall_0;ft_;pt_1'
    Fund_info_url = 'https://fund.eastmoney.com/'
    output_file = 'D:/DayDayFund.txt'
    slist = []
    getFundList(slist, Fund_list_url)
    getFundInfo(slist, Fund_info_url, output_file)


main()


  • 记录学习
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值