雪球爬取数据

爬取数据

from urllib import request
import json
# url
url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id={}&count={}&category=111'

def Snowball(page,max_id=None,count=None):
    # 请求头
    headers = {
        'Cookie': 'aliyungf_tc=AQAAAFIZtRD3fgIAUhVFeaXEDQJdCP+S; xq_a_token=584d0cf8d5a5a9809761f2244d8d272bac729ed4; xq_a_token.sig=x0gT9jm6qnwd-ddLu66T3A8KiVA; xq_r_token=98f278457fc4e1e5eb0846e36a7296e642b8138a; xq_r_token.sig=2Uxv_DgYTcCjz7qx4j570JpNHIs; _ga=GA1.2.1087044515.1534297298; _gid=GA1.2.1740003293.1534297298; u=151534297299198; device_id=0c55f25e05c68b0c012feaab18b5d894; Hm_lvt_1db88642e346389874251b5a1eded6e3=1534297300,1534297331; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1534297331',
        'Referer': 'https://xueqiu.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36',
    }
    if max_id is None:
        full_url = url.format(-1,10)
    else:
        full_url = url.format(max_id,count)
    count = 15
    if page<4:
        # 请求对象
        req = request.Request(full_url,headers=headers)
        page += 1
        response = request.urlopen(req)
        html_bytes = response.read().decode('utf-8')
        res_dict = json.loads(html_bytes)
        list1 = res_dict['list']
        # print(res_dict)
        for item  in list1:
            data_str = item['data']
            print(item['id'],data_str)
        Snowball(page,item['id'],count)
if __name__ == "__main__":
    Snowball(1,-1,10)
根据提供的引用内容,你可以使用以下步骤来爬取雪球网站的数据: 步骤1: 导入所需的库 ```python import pandas as pd from bs4 import BeautifulSoup import re import requests import json ``` 步骤2: 定义下载网页的方法 ```python def download_page(url, para=None): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.59' } if para: response = requests.get(url, params=para, headers=headers) else: response = requests.get(url, headers=headers) response.encoding = response.apparent_encoding if response.status_code == 200: return response.text else: print("failed to download the page") ``` 步骤3: 解析网页并提取所需数据 ```python def parse_page(html): # 使用BeautifulSoup解析网页 soup = BeautifulSoup(html, 'html.parser') # 根据网页结构和需要的数据,使用CSS选择器或正则表达式进行提取 # 例如: # title = soup.select('.title')[0].text # content = soup.select('.content')[0].text # 返回提取的数据 # return title, content ``` 步骤4: 执行爬虫 ```python def run_spider(): url = 'https://xueqiu.com' # 根据网页结构和需要的参数,构造请求参数 # 例如: # para = {'param1': 'value1', 'param2': 'value2'} # 下载网页 html = download_page(url, para) # 解析网页并提取数据 # title, content = parse_page(html) # 输出提取的数据 # print('Title:', title) # print('Content:', content) ``` 请注意,这只是一个简单的示例,你可能需要根据雪球网站的具体结构和需求进行适当的修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值