Python scrapy可以根据id提交输入吗?

Python scrapy可以根据id提交输入吗?,python,beautifulsoup,scrapy,Python,Beautifulsoup,Scrapy,我有一个具有多个输入字段的Intranet页面,我需要Scrapy使用网页“搜索产品”输入字段运行搜索,它的id为“searchBox” 我已经能够使用Scrapy和Beautiful Soup锁定正确的搜索框,但我不确定如何将该数据正确地传递回Scrapys表单提交功能 在方法1中,我试图简单地将结果作为输入传递给Scrapys FormRequest.from_响应函数,但它不起作用 方法1-使用Scrapy查找数据 #Search for products def parse(self,

我有一个具有多个输入字段的Intranet页面,我需要Scrapy使用网页“搜索产品”输入字段运行搜索,它的id为“searchBox”

我已经能够使用Scrapy和Beautiful Soup锁定正确的搜索框,但我不确定如何将该数据正确地传递回Scrapys表单提交功能

在方法1中,我试图简单地将结果作为输入传递给Scrapys FormRequest.from_响应函数,但它不起作用

方法1-使用Scrapy查找数据

#Search for products
def parse(self, response):

    ##Let's try search using scrapy only
    sel = Selector(response)
    results = sel.xpath("//*[contains(@id, 'searchBox')]")
    for result in results:
        print (result.extract())   #Print out what scrapy found
    return scrapy.FormRequest.from_response(results, formdata = {'Item': 'Whirlpool Washing Machine'}) #formdata is the data we are sending
#Search for products
def parse(self, response):

    ##Let's try search using Beautiful Soup only
    soup = BeautifulSoup(response.text, 'html.parser')  
    product_search = []
    product_search.append(soup.find("input", id="searchBox")) 
    print(product_search) #Print what BS found
方法2-使用Beautiful soup查找数据

#Search for products
def parse(self, response):

    ##Let's try search using scrapy only
    sel = Selector(response)
    results = sel.xpath("//*[contains(@id, 'searchBox')]")
    for result in results:
        print (result.extract())   #Print out what scrapy found
    return scrapy.FormRequest.from_response(results, formdata = {'Item': 'Whirlpool Washing Machine'}) #formdata is the data we are sending
#Search for products
def parse(self, response):

    ##Let's try search using Beautiful Soup only
    soup = BeautifulSoup(response.text, 'html.parser')  
    product_search = []
    product_search.append(soup.find("input", id="searchBox")) 
    print(product_search) #Print what BS found
关于刮痧变种:

  • 您应该
    生成
    请求,而不是
    返回
  • 在函数
    from_response
    中,应使用形式选择器作为第一个参数。现在,您将传递一些输入数据,据我从您的代码中了解 尝试以下方法:

    yield scrapy.FormRequest.from_response(response.css('form'), formdata={'Item': 'Whirlpool Washing Machine'})
    
    只需修复此表达式中的表单选择器。还要检查此请求中还应使用哪些内容,可能是一些标题、cookie等。

    关于scrapy变体:

  • 您应该
    生成
    请求,而不是
    返回
  • 在函数
    from_response
    中,应使用形式选择器作为第一个参数。现在,您将传递一些输入数据,据我从您的代码中了解 尝试以下方法:

    yield scrapy.FormRequest.from_response(response.css('form'), formdata={'Item': 'Whirlpool Washing Machine'})
    

    只需修复此表达式中的表单选择器。还请检查此请求中还应使用哪些内容,可能是一些标题、cookie等。

    我认为输入是由JavaScript动态生成的,因为当我在Chrome中刷新页面时,浏览器中的输入名称不同。只有您可以看到它,因为我们无法访问您的intranet页面。我们只能建议可能的错误。我认为输入是由JavaScript动态生成的,因为当我在Chrome中刷新页面时,浏览器中的输入名称不同。只有您可以看到它,因为我们无法访问您的intranet页面。我们只能提出可能出错的地方。