需求如题:第一步就卡住了,request.get回来的文件里没有任何基金经理页面的信息。
在network 的all里,也没有发现发现基金经理的信息。
请高手指点一下,爬虫怎么开始。
举报 使用道具
| 回复

共 1 个关于本帖的回复 最后回复于 2021-12-1 17:13

aixce1 新手上路 发表于 2021-12-1 17:13:00 | 显示全部楼层
[ 本帖最后由 aixce1 于 2021-12-1 17:14 编辑 ]\n\n[ 本帖最后由 aixce1 于 2021-12-1 17:14 编辑 ]\n\n

async def getBrief(url,header):
    async with aiohttp.ClientSession(headers=header) as session:
        async with session.get(url) as response:
            text = await response.content.read()
            text = text.decode()
            item = {}
            title = re.findall(r'<title>(.*?) _ 基金经理档案 _ 天天基金网</title>',text)
            brief= re.findall(r'<span class="strong">基金经理简介:</span>(.*?)</p>', text,re.S)
            if title:
                item['title'] = title[0]
                item['brief']  = brief[0]
                async with aiofiles.open(f'基金经理简介.txt', mode='a',encoding='utf-8') as f:
                    await f.write(json.dumps(item,ensure_ascii=False) + '\n')


async def getDetailPages(url,header ):
    async with aiohttp.ClientSession(headers=header) as session:
        async with session.get(url) as response:
            text = await response.content.read()
            text = re.findall(r'var returnjson= (.*?),record:2825,pages:.*,curpage:.*}',text.decode())[0]
            text = text + "}"
            text = text.replace('data','"data"')
            data = json.loads(text)
            task = []
            for item in data['data']:
                detail_id = item[0]
                detail_url = f"http://fund.eastmoney.com/manager/{detail_id}.html"
                task.append(getBrief(detail_url,header))
            await asyncio.wait(task)


async def getUrl(header):
    task = []
    for page in range(1,20):
        url = "http://fund.eastmoney.com/Data/FundDataPortfolio_Interface.aspx?dt=14&mc=returnjson&ft=all&pn=50&pi={page}&sc=abbname&st=asc".format(page=page)
        task.append(getDetailPages(url,header))
    await asyncio.wait(task)

def main():
    header = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
         (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"}
    loop = asyncio.get_event_loop()
    loop.run_until_complete(getUrl(header))



if __name__ == '__main__':
    main()

Snipaste_2021-12-01_17-12-20.png
举报 使用道具
您需要登录后才可以回帖 登录 | 立即注册

精彩推荐

  • Gephi社会网络分析-马蜂窝游记文本分词并同
  • Gephi社会网络分析-基于马蜂窝游记文本以词
  • 知乎话题文本根据词语间距筛选后生成共词矩
  • 马蜂窝游记文本分词后以词语间距为筛选条件
  • 学习使用apriori算法挖掘关联关系

热门用户

GMT+8, 2024-4-17 02:53