You need to enable JavaScript to run this app. 如何使用Python采集GET链接并逐个跳转提取详情页数据
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python采集GET链接并逐个跳转提取详情页数据

问题原因

现有代码的页面采集逻辑放在了URL遍历的for循环之外,循环过程中仅会不断覆盖url变量的值,循环结束后仅会用最后一个url执行一次采集,因此只能得到一条结果。

修正方案

把采集每个页面信息的逻辑缩进,放到for循环内部,保证每遍历到一个新url,就同步执行一次采集操作,修正后代码如下:

# 请确保已经提前导入依赖库
import requests
from bs4 import BeautifulSoup

# collect urls
# 此处html为你已经解析完成的列表页BeautifulSoup对象
data = html.select("div.content a")
for i in data:
    url = "wwww...." + i["href"]
    # 采集逻辑放到for循环内部,每个url都会执行一次采集
    raw_each = requests.get(url, headers={"User-Agent":"Mozilla/5.0"})
    html_each = BeautifulSoup(raw_each.text,'html.parser')
    reply = html_each.select("div.content td")
    print(reply[14].text)

可选优化

如果需要后续处理所有采集到的内容,可以提前定义空列表存储结果,避免仅打印输出无法复用:

import requests
from bs4 import BeautifulSoup

result = []
data = html.select("div.content a")
for i in data:
    url = "wwww...." + i["href"]
    raw_each = requests.get(url, headers={"User-Agent":"Mozilla/5.0"})
    html_each = BeautifulSoup(raw_each.text,'html.parser')
    reply = html_each.select("div.content td")
    result.append(reply[14].text)

# 所有采集结果统一存储在result列表中
print(result)

内容的提问来源于stack exchange,提问作者JUNG UN LEE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 06:36:03