以下是使用Python的代码来访问网页并获取数据并翻页:

import requests
from bs4 import BeautifulSoup
import csv

url = "https://data.10jqka.com.cn/financial/yjyg/"
filename = "data/yjyg.csv"

# 获取表头列名
def get_header(soup):
    table = soup.find('table', class_='m-table J-ajax-table')
    thead = table.thead
    headers = []
    for th in thead.find_all('th'):
        headers.append(th.text.strip())
    return headers

# 获取表格数据
def get_table_data(soup):
    data = []
    table = soup.find('table', class_='m-table J-ajax-table')
    tbody = table.tbody
    for tr in tbody.find_all('tr'):
        row = []
        for td in tr.find_all('td'):
            row.append(td.text.strip())
        data.append(row)
    return data

# 将数据写入csv文件
def write_to_csv(data, filename):
    with open(filename, 'a', newline='', encoding='utf-8') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerows(data)

# 访问网页并获取数据
def get_data(url, filename):
    res = requests.get(url)
    soup = BeautifulSoup(res.text, 'html.parser')
    headers = get_header(soup)
    data = get_table_data(soup)
    write_to_csv(data, filename)
    return headers, data

# 获取下一页的链接地址
def get_next_page_url(soup):
    pager = soup.find('div', class_='m-page J-ajax-page')
    next_page = pager.find('a', class_='page-next')
    if next_page:
        return next_page['href']
    else:
        return None

# 翻页并获取数据
def get_all_data(url, filename):
    headers, data = get_data(url, filename)
    print(headers)
    while True:
        next_page_url = get_next_page_url(BeautifulSoup(requests.get(url).text, 'html.parser'))
        if not next_page_url:
            break
        url = "https://data.10jqka.com.cn" + next_page_url
        headers, data = get_data(url, filename)
        print(headers)

get_all_data(url, filename)

这个脚本将会访问给定的url,获取表格的列名和数据,并将它们写入到一个CSV文件中。它会检查是否有下一页的链接地址,如果有,就继续翻页并获取数据,直到没有下一页为止。

用python 访问 httpsdata10jqkacomcnfinancialyjyg获取网页里面的数据其中寻找下一页的标签实现翻页获取下一页的数据完成以后写入到datayjygcsv的文件中

原文地址: https://www.cveoy.top/t/topic/8F4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录