python - 进一步抓取链接
问题描述
我目前正在尝试抓取https://rl.insider.gg/en/xbox并尝试识别页面上的“趋势”项目
我以某种方式达到了我拥有链接的地步,但无法弄清楚如何进一步解析它们。我想要并且想要从中提取最后一部分并将这两个值存储在两个变量中:颜色和项目
例如:(我现在拥有的链接示例)
/en/xbox/octane/white
我想去掉 en 和 xbox 位,只留下octane/white
. 然后,将 存储octane
为名为 item 的变量,并将 存储为white
名为 color 的变量
到目前为止我所拥有的
import requests
from bs4 import BeautifulSoup
page = requests.get("https://rl.insider.gg/en/xbox")
soup = BeautifulSoup(page.content, 'html.parser')
trendingitems = soup.find(id="trendingItems")
for link in trendingitems.find_all('a'):
linkitems = (link.get('href'))
print(linkitems)
解决方案
尝试这个
import requests
from bs4 import BeautifulSoup
page = requests.get("https://rl.insider.gg/en/xbox")
soup = BeautifulSoup(page.content, 'html.parser')
trending_items = soup.find(id="trendingItems")
for link in trending_items.find_all('a'):
link_items = link.get('href').replace('/en/xbox/', '')
split_items = link_items.split('/')
if len(split_items) == 2:
item = split_items[0]
colour = split_items[1]
我计算 split_items 因为一些返回值没有颜色。如果我得到 2 的结果,我们就知道我们已经有了商品和颜色。
octane/white
zomba/white
fennec
fennec/white
dissolver
mainframe
interstellar
octane/crimson
dueling_dragons
standard/black
emerald/white
mainframe/white
octane/sblue
big_splash
octane/lime
stipple_gait
emerald/black
helios/white
interstellar/white
standard/white
20xx
dieci_uncommon/black
fire_god
heatwave
zomba
推荐阅读
- python - 如何提高熊猫数据框的列表理解速度
- node.js - 来自域名的 Nuxt BaseUrl
- vue.js - watch 对象的 Vue.js 处理程序属性
- c - 我在c编程中的for循环中读到了3个用冒号(;)分隔的表达式,但是这个问题测试表达式被省略了,这可能吗?
- java - 为什么我的 CompletableFuture 代码在 Java 8 中运行而不在 Java 11 中运行?
- c# - 如何确保在第一个 addphoto 行完成后调用第二个产品 addPhoto 方法?
- python - 如何在非二叉树python中找到路径
- powershell - 在powershell中组合csv文件
- python - 使用 openCV 保存的视频不显示任何内容
- c# - 为什么这个 azure 函数会产生 JSON 输出?