前方的灯 2022-01-13 00:00 采纳率: 50%
浏览 28

爬取同站不同页面代码竟只返回相同结果

无论urls怎么变,传入到craw请求网页,总是获取到第一页的代码,甚至直接手写网址一样都是第一页的代码

import requests
from lxml import etree

urls =[
    f"https://www.cnblogs.com/#p{page}"
    for page in range(1,50+1)
]

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36",
    "referer": "https://www.cnblogs.com/"
}
def craw(u1):
    rep = requests.get(u1,headers=headers)
    return rep.text



# title ='//article[@class='post-item']//a/text()'
# href_u = '//article[@class='post-item']//a/@href'
def Processors(txt):
    x1 ="//article[@class='post-item']//a[@class='post-item-title']/text()"
    x2 = "//article[@class='post-item']//a/@href"
    html = etree.HTML(txt)

    title = html.xpath(x1)
    href_u = html.xpath(x2)
    print(txt)

# Processors(craw(urls[2]))         #爬取第3页,返回第一页代码
print(craw("https://www.cnblogs.com/#p3"))      #爬取第3页,返回第一页代码
print("------------------------------------------------------------------")
print(craw("https://www.cnblogs.com/#p5"))      #爬取第5页,返回第一页代码


# -------------
# 从craw函数请求url后的结果一直都第一页代码
# 反复验证了Processors函数没毛病。
我专门手写了2个不一样的url请求,在运行返回中同时寻找第一页中的标题,有返回2个,其他页的标题页均无发现
  • 写回答

2条回答 默认 最新

  • Vae 东 2022-01-13 09:43
    关注

    这个网站页面的翻页数据是通过接口的

    img

    img

    评论

报告相同问题?

问题事件

  • 创建了问题 1月13日

悬赏问题

  • ¥15 有人会用py或者r画这种图吗
  • ¥15 MOD04_3K图像预处理
  • ¥15 [VASP]关于超胞大小和k 点网格的收敛性测试
  • ¥15 pip下载paddle2onnx离谱错误
  • ¥60 db2move nlzxams import 导出db2备份数据报错
  • ¥15 关于#python#的问题:全文总结功能咨询
  • ¥15 俄罗斯方块中无法同时消除多个满行
  • ¥15 c#转安卓 java html
  • ¥15 使用gojs3.0,如何在nodeDataArray设置好text的位置,再go.TextBlock alignment中进行相应的改变
  • ¥15 psfusion图像融合指标很低