gpt4 book ai didi

python - 使用xpath解析出不需要的数据

转载 作者:行者123 更新时间:2023-12-01 07:39:15 26 4
gpt4 key购买 nike

我正在使用 Scrapy 来抓取网站。访问该网站后,我需要获取每个类别的 id 值,并使用该值重定向到我需要抓取的数据所在的 JSON 网页。下图中是 HTML 代码的部分快照,显示了我需要的类别及其值 ID 号。我需要该 id 值,以便可以将其插入此 url 的末尾并重定向到该 url。 "http://www.starcitygames.com/buylist/search?search-type=category&id= "我需要对所有类别执行此操作。我有下面的代码,但现在使用 xpath 来获取这些 id,但它一次返回整个 Id 列表,而不是一次返回一个。它为我提供了我不需要的其他数据。

HTML Code

Currently what I am receiving for category_id

import scrapy
import json
from scrapy.spiders import Spider
from scrapy_splash import SplashRequest
from ..items import NameItem

class LoginSpider(scrapy.Spider):
name = "LoginSpider"
start_urls = ["http://www.starcitygames.com/buylist/"]

def parse(self, response):
return scrapy.FormRequest.from_response(
response,
formcss='#existing_users form',
formdata={'ex_usr_email': 'email@example.com', 'ex_usr_pass': 'passowrd'},
callback=self.after_login
)



def after_login(self, response):
item = NameItem()
category_id = response.xpath('//*[@id="bl-category-options"]/option/@value')

最佳答案

非常简单:

for catetegory_id in response.xpath('//select[@id="bl-category-options"]/option/@value').getall():
yield scrapy.Request(
url="http://www.starcitygames.com/buylist/search?search-type=category&id={category_id}".format(category_id=category_id),
callback=self.parse_json_response,
)

关于python - 使用xpath解析出不需要的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com