网页肯定有40行数据为什么能获取到部分行?

问题描述 投票:0回答:0

webapi "vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/profit/index.phtml" ,提供

get
方法调用的query,每页40行分页。 我写了一个函数来调用 webapi 并打印网页中的所有行:

def get_rows(page):
    import urllib.request
    import lxml.html
    url = "http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/profit/"\
          "index.phtml?s_i=&s_a=&s_c=&reportdate=2021&quarter=4&p={}".format(page)
    table_xpath = '//*[@id="dataTable"]'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
    req = urllib.request.Request(url=url, headers=headers)
    data_string=urllib.request.urlopen(req).read()
    root=lxml.html.fromstring(data_string)
    dtable = root.xpath(table_xpath)[0]
    rows = dtable.xpath('.//tr')
    print(len(rows))

现在称它为:

get_rows(page=1)
41
get_rows(page=2)
41
get_rows(page=3)
26
get_rows(page=4)
41

为什么当网页确实包含40行(41=1个标题+40行数据)时,我的函数只能获取第3页的部分行(26)? 我发现很多页面都遇到了同样的问题,wbeoage 包含 40 行数据,get_rows() 打印的数字小于 40。请尝试使用我的函数:

[get_rows(page) for page in [3,38,73,81,118,123]]
python-3.x web-crawler
© www.soinside.com 2019 - 2024. All rights reserved.