python网络爬虫之每日股票交易信息

import re
import asyncio
from bs4 import BeautifulSoup
import pyppeteer as pyp
async def antiAntiCrawler(page):   #为page添加反反爬虫手段
    await page.setUserAgent('Mozilla/5.0 (Windows NT 6.1; \Win64; x64)')
    await page.evaluateOnNewDocument('()=>{Object.defineProperties(navigator,\{webdriver:{get:()=>false}})}')
async def getStockCodes(page):
    codes=[]
    html=await page.content()
    soup=BeautifulSoup(html,"html.parser")
    for x in soup.find_all("li"):
        a=x.find("a")
        if ("(" in a.text and ")" in a.text):
            codes.append(a.text)
    return codes
async def getStockInfo(url):
    broser = await pyp.launch(headless=False, executablePath='D:\chromium\chrome-win\chrome.exe')   #启动Chromium,非隐藏启动,路径根据自己情况而定
    page = await broser.newPage() #在浏览器中打开一个新页面
    await antiAntiCrawler(page)
    await page.goto(url)   #装入url对应的网页
    codes=await getStockCodes(page)
    for x in codes[:3]:
        print("-----",x)
        pos1,pos2=x.index("("),x.index(")")
        code=x[pos1+1:pos2]
        url="http://quote.eastmoney.com/sh"+code+".html"
        await page.goto(url)
        html=await page.content()
        pt=r'<td>([^<]*)</td>.*?<td[^>]*id="gt\d*?"[^>]*>([^<]*)</td>'
        for x in re.findall(pt,html,re.DOTALL):
            print(x[0],x[1])
    await broser.close()
url="https://www.banban.cn/gupiao/list_sh.html"
loop=asyncio.get_event_loop()
loop.run_until_complete(getStockInfo(url))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lin_Zhenyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值